3

I am getting an error saying that ID was already declared, so it cannot be declared again in my while loop. Then how do I increment my loop?

int RealID = 100;
Console.WriteLine("Enter Number");
int ID = int.Parse(Console.ReadLine());

while( ID != ReadID)
{
    Console.WriteLine("Incorrect ID. Enter another number");
    int ID = int.Parse(Console.ReadLine());
}
Console.WriteLine("You entered the correct ID");

2 Answers 2

1

As the error states, don't declare it again... just assign the new value:

while (ID != ReadID)
{
    Console.WriteLine("Incorrect ID. Enter another number");
    ID = int.Parse(Console.ReadLine());
}
Sign up to request clarification or add additional context in comments.

Comments

1

By prefacing your variable ID with a type, you are in fact re-declaring it.

Reuse it instead of re-declaring it by assigning the new value to it.

int RealID = 100;
Console.WriteLine("Enter Number");
int ID = int.Parse(Console.ReadLine());

while( ID != ReadID)
{
    Console.WriteLine("Incorrect ID. Enter another number");
    ID = int.Parse(Console.ReadLine());
}
Console.WriteLine("You entered the correct ID");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.