Read an undefined number of lines from standard input

c#

Solution

List<int> input = new List<int>();

// As long as there are nonempty items read the input
// then add the item to the list    
string line;
while ((line = Console.ReadLine()) != null && line != "") {
     input.Add(int.Parse(line));
}

// To access the list elements simply use the operator [], instead of ElementAt:
StockItem[] stock = new StockItem[input.Count];
for (int i = 0; i < stock.Length; i++) {
    stock[i] = new StockItem(input[i]);
}

Problem

I have an unknown number of lines of input. I know that each line is an integer, and I need to make an array with all the lines, for example: Input: ``` 12 1 3 4 5 ``` and I need to get it as an array: `{12,1,3,4,5}` I have the below code, but I can't get all the lines, and I can't debug the code because I need to send it to test it. ``` List<int> input = new List<int>(); string line; while ((line = Console.ReadLine()) != null) { input.Add(int.Parse(Console.In.ReadLine())); } StockItem[] stock = new StockItem[input.Count]; for (int i = 0; i < stock.Length; i++) { stock[i] = new StockItem(input.ElementAt(i)); } ```

Original source

Related problems