need help please
-
-
for (int i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}
Console.Write(i);
Console.ReadKey();how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.
Put the console.write and console.readkey INSIDE the for next loop. Move the } down 2 lines.
Never underestimate the power of human stupidity RAH
-
for (int i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}
Console.Write(i);
Console.ReadKey();how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.
problem in this code is that 'i' is only defined for 'for loop ' to make it run u have to define i before for loop ex: int i = new int(); for (i = 0; i < 5; i++) { i = int.Parse(Console.ReadLine()); } Console.Write(i); Console.ReadKey(); it will not give any error but don't give u the result u want it should be like this.. static void Main(string[] args) { int i = new int(); int []arry=new int[5]; for (i = 0; i < 5; i++) { arry[i] = int.Parse(Console.ReadLine()); } for (i = 0; i < 5; i++) { Console.Write(arry[i]+"\n"); } Console.ReadKey(); }
-
Put the console.write and console.readkey INSIDE the for next loop. Move the } down 2 lines.
Never underestimate the power of human stupidity RAH
-
problem in this code is that 'i' is only defined for 'for loop ' to make it run u have to define i before for loop ex: int i = new int(); for (i = 0; i < 5; i++) { i = int.Parse(Console.ReadLine()); } Console.Write(i); Console.ReadKey(); it will not give any error but don't give u the result u want it should be like this.. static void Main(string[] args) { int i = new int(); int []arry=new int[5]; for (i = 0; i < 5; i++) { arry[i] = int.Parse(Console.ReadLine()); } for (i = 0; i < 5; i++) { Console.Write(arry[i]+"\n"); } Console.ReadKey(); }
-
i've tried that, it just writes the numbers straight after the entering, i want it to write all 5 AFTER i've entered the fifth number.
And that make no sense at all, you would have to put the numbers into a container (array/List<>) and then loop through the container and print them!
Never underestimate the power of human stupidity RAH
-
for (int i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}
Console.Write(i);
Console.ReadKey();how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.
Just to add to what the others have said - you don't want to do it like that anyway!
for (int i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}Since
i
is both the loop control variable and the place you store the value the user entered, if the user enters 4 or above as his first number, it will exit the loop immediately. You need to separate these two functions into two variables, as well as using an array or List<int> BTW: It is considered a bad idea to use "magic numbers" such as "5" in your code - when you move to an array, it is too easy to later change one and not the other, causing your application to fail, or crash:int[] inp = new int[5];
for (int i = 0; i < 5; i++)
{
inp[i] = int.Parse(Console.ReadLine());
}would be better as:
const int elements = 5;
int[] inp = new int[elements];
for (int i = 0; i < elements; i++)
{
inp[i] = int.Parse(Console.ReadLine());
}That way, you only have to change the number of elements in a single place, and it all works perfectly. And as a final thing, if your user enters an alphabetic character instead of a digit, your program will crash. You should always check your user input - they do enter rubbish quite often! :laugh: [edit]Typos - OriginalGriff[/edit]
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
for (int i = 0; i < 5; i++)
{
i = int.Parse(Console.ReadLine());
}
Console.Write(i);
Console.ReadKey();how to get console to print all of the numbers entered (5 numbers)? when i debug this it says that the name 'i' does not exist in current context.
i wonder how this programs builds without any error? there are few things you need to check here 1.the variable i is accessible only inside the loop and its local to that loop never accessible outside the loops. 2. the indexer variable i is modified against your input which may alter the loop execution 3. Console.Write is outside the loop so if variable i is acceible outside the loop it will print only the last entered value not all try modifying your code. define a new variable to store the read line and move the Console.Write inside the loop and see the difference
Jibesh.V.P India