+ 3

How to repeatedly use ReadLine in c#?

I'm trying to make a program that asks the user for 6 numbers and then does something with them but deadline will only get input for the first variable and ignore the rest of the script (ps: I'm using Convert.ToInt32 with the readline)

20th Jan 2017, 12:32 AM
knowledgekeeper
knowledgekeeper - avatar
14 Réponses
+ 11
With the way the online compiler works, you must send all inputs your code needs at once. So when you get the dialogue box for input, write all 6 inputs separated by new lines. When your program is run, it takes those inputs one line at a time, as though a user were inputting them.
20th Jan 2017, 12:45 AM
Tamra
Tamra - avatar
+ 4
Have you tried Console.WriteLine("Enter some1"); var some1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter some2"); var some2 = Convert.ToInt32(Console.ReadLine()); etc...
20th Jan 2017, 12:58 AM
Aaron
Aaron - avatar
+ 3
@Erwin Messias Won't work. foreach-variables are get-only.
21st Jan 2017, 10:15 AM
Maike
+ 2
int[] arr = new int[6]; foreach(int i in arr) i = Convert.ToInt32(Console.ReadLine());
21st Jan 2017, 2:49 AM
Erwin Mesias
Erwin Mesias - avatar
+ 2
Let me give you two methods of doing this METHOD 1. Use a For Loop and ask for each variable value on a new line for number of variables(x) you want Static void main() { for(int i = 1; i < =x; i++) //x is the number of times you want to get input, in this case of yours x =6 { int Num = 0; Console.Write("Enter the i value "); // i means for each time first, second until the Last Num =Convert.Toint32(Console. ReadLine ()); //stores the value for each interval //input your code to perform operation and print Output } } METHOD 2. Get all your input values on the same line using split method by spacing them, save them as arrays and use for loop to access them Static Void Main { Console.Write("input x variable values on same line using single Space"); //x is the number of times you want to get values, in your case x = 6 int[] inputs = Console.ReadLine().Split(" "); // in this case, I used space as my split Int [] nums = new int[x]; //create a int array to save them since your inputs are integers For (int i=0; i<x, i++) { nums[i] = Convert.Toint32(inputs[i]); //converts your inputs which are in string to integers } //perform your operation by using for loop or for each to access your values }
21st Jan 2017, 4:28 PM
HayHiTee
HayHiTee - avatar
+ 1
if it says deadline I mean readline but my phone auto corrected it sorry
20th Jan 2017, 12:33 AM
knowledgekeeper
knowledgekeeper - avatar
+ 1
make list of int and for loop. In the loop read user's input and add the converted number into the list. After loop make whatever you want.
20th Jan 2017, 3:52 PM
Kamil Kosyl
Kamil Kosyl - avatar
+ 1
have you used try...catch around your convert.toint32? It might be throwing an exception that could prevent further iterations of your loop. This works fine for me(just be sure to use the enter key to enter 5 lines of input): List<string> input=new List<string>(); List<int> numbers=new List<int>(); for(var i=0;i<5;i++) input.Add((string)Console.ReadLine()); foreach(var line in input) { try { numbers.Add(Convert.ToInt32(line)); } catch(Exception e) { Console.WriteLine(e.Message); } } foreach(var number in numbers) Console.WriteLine(number);
21st Jan 2017, 8:52 AM
Niels Koomen
Niels Koomen - avatar
+ 1
@Aaron yeah it still does one input
21st Jan 2017, 11:51 AM
knowledgekeeper
knowledgekeeper - avatar
+ 1
@Tamra thanks!
21st Jan 2017, 11:52 AM
knowledgekeeper
knowledgekeeper - avatar
+ 1
Try this, it will let you use ReadLine(); according to the value of x. int main() { Console.Write("Number of ReadLines: "); int x = Convert.ToInt32(Console.ReadLine()); // Prompts you to enter the number of ReadLines you want to do int num = 0; for(int i = 0; i < x; i++) // For loop that repeats ReadLine according to value of x { Console.WriteLine("#" + i + ": "); num = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine(num); // Displays all values stored inside the num variable Console.ReadKey(); }
21st Jan 2017, 3:18 PM
John Clark
John Clark - avatar
+ 1
@John Clark the code wont work since you cannot access num variable outside the loop and even if you could, it would store the last number given in the loop by user
21st Jan 2017, 3:58 PM
Kamil Kosyl
Kamil Kosyl - avatar
+ 1
@Kayx ohh, I did not know that :) Sorry, my bad :) I did not try it, I just thought that would work. XD But the logic is there, just simply replace foreach with for with var from 0 to < arr.length. Access each element of the array then equate them with your int.Parse(C.RL()):
21st Jan 2017, 6:13 PM
Erwin Mesias
Erwin Mesias - avatar
+ 1
thanks for the help guys! if you want to see what I was using it for check my code age comparer
22nd Jan 2017, 12:59 AM
knowledgekeeper
knowledgekeeper - avatar