+ 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)
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.
+ 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...
+ 3
@Erwin Messias
Won't work. foreach-variables are get-only.
+ 2
int[] arr = new int[6];
foreach(int i in arr)
i = Convert.ToInt32(Console.ReadLine());
+ 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
}
+ 1
if it says deadline I mean readline but my phone auto corrected it sorry
+ 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.
+ 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);
+ 1
@Aaron yeah it still does one input
+ 1
@Tamra thanks!
+ 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();
}
+ 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
+ 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()):
+ 1
thanks for the help guys! if you want to see what I was using it for check my code age comparer