Kisangi Kisangi -4 years ago 76
C# Question

Very basic bingo game

I'm new to C# and programming as a whole and i have a quick question to ask you guys, I've searched a bit but only found far too complicated examples for me to implement in my work so here goes:

int[] newArray = new int[7];
Console.WriteLine("Hello! Please enter 7 numbers between 1-25, press ENTER after each number. ");
for (int i = 0; i < newArray.Length; i++)
bool loop = true;
do
{
try
{
newArray[i] = Convert.ToInt32(Console.ReadLine());
loop = false;
}
catch
{
Console.WriteLine("You may only enter numbers!");
}
} while (loop);
Console.Write("You entered the following numbers: ");

for (int i = 0; i < newArray.Length; i++)
{
Console.WriteLine(newArray[i]);
}
}


This is the first part of a bingogame im trying to write, but i can't understand why the names loop and i don't exist, should i make something static? Move some brackets around? Please help.

Answer Source

You need to wrap the entire for statement in braces, otherwise it will only execute the next line of code, which is just bool loop = true;.

for (int i = 0; i < newArray.Length; i++)
{ // <-- Add this
  bool loop = true;
  do
  {
    try
    {
      newArray[i] = Convert.ToInt32(Console.ReadLine());
      loop = false;
    }
    catch
    {
      Console.WriteLine("You may only enter numbers!");  
    }
  } while (loop);

  Console.Write("You entered the following numbers: ");

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download