Reputation: 866
my code:
string[] code = new string[9];
int[] intCode = new int[9];
int cd = 0, dvd = 0, video = 0, book = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("Enter code#{0}: ",i+1);
code[i] = Console.ReadLine();
if (code[i].Length==5)
{
intCode[i] = Convert.ToInt32(code[i]);
intCode[i] /= 100000;
if (intCode[i] == 1)
{
cd++;
break;
}
if (intCode[i] == 2)
{
dvd++;
break;
}
if (intCode[i] == 3)
{
video++;
break;
}
if (intCode[i] == 4)
{
book++;
break;
}
}
else
{
Console.WriteLine("INVALID CODE");
}
}
Basically what i want to do is else{do some thing here} to ask the user to reinput the number, instead of going to for loop and icrementing i and asking the user for new input.
Upvotes: 0
Views: 649
Reputation:
Use a combination of while and switch:
string[] code = new string[9];
int[] intCode = new int[9];
int cd = 0, dvd = 0, video = 0, book = 0;
for (int i = 0; i < 10; i++)
{
bool isCorrectInput = false;
while (!isCorrectInput)
{
isCorrectInput = true;
Console.Write("Enter code#{0}: ", i+1);
code[i] = Console.ReadLine();
if (code[i].Length == 1)
{
intCode[i] = Convert.ToInt32(code[i]);
// intCode /= 100000;
switch (intCode[i])
{
case 1:
cd++;
break;
case 2:
dvd++;
break;
case 3:
video++;
break;
case 4:
book++;
break;
default:
isCorrectInput = false;
break;
}
}
else
isCorrectInput = false;
if (!isCorrectInput)
Console.WriteLine("INVALID CODE ENTERED!");
}
}
EDIT: Should be what you want now, also corrected your bugs
Upvotes: 0
Reputation: 1008
In the else block:
else
{
Console.WriteLine("INVALID CODE");
i -= 1;
}
Upvotes: 1