Reputation:
So I'm just asking the user their name and then asking the question again if it was deemed incorrect by the user. But after they have said it is wrong I would like to say something like "Apologies, what is the correct name?". But putting it in the do while loop would make it say that the first time which doesn't make any sense. I would like for it to say it after the first incorrect input and every time after.
string nameCorrect;
string playerName;
do
{
Console.WriteLine("What is your name?");
playerName = Console.ReadLine();
Console.WriteLine("Is {0} correct?", playerName);
nameCorrect = Console.ReadLine();
} while(nameCorrect == "No");
if(nameCorrect == "Yes")
{
Console.WriteLine("Great, lets move on.");
}
Console.ReadKey();
Upvotes: 0
Views: 56
Reputation: 53
string nameQuestion = "What is your name?";
string name = string.Empty;
bool needName = true;
do {
Console.WriteLine(nameQuestion);
name = Console.ReadLine();
Console.WriteLine($"Is {name} correct?");
if (Console.ReadLine().ToLower().Equals("yes")) {
needName = false;
}
else {
nameQuestion = "Apologies, what is the correct name?";
}
} while (needName);
Console.WriteLine("Great, lets move on.");
Console.ReadKey();
Upvotes: 0
Reputation: 972
Just keep track of how many times they attempted it:
string nameCorrect;
string playerName;
int attempt = 0;
do
{
Console.WriteLine(attempt > 0
? "Apologies, what is the correct name?"
: "What is your name?");
playerName = Console.ReadLine();
Console.WriteLine("Is {0} correct?", playerName);
nameCorrect = Console.ReadLine();
attempt++;
} while(nameCorrect == "No");
if(nameCorrect == "Yes")
{
Console.WriteLine("Great, lets move on.");
}
Console.ReadKey();
Upvotes: 0
Reputation: 20373
Just use a variable, which you can update after the first iteration:
string message = "What is your name?";
do
{
Console.WriteLine(message);
playerName = Console.ReadLine();
Console.WriteLine("Is {0} correct?", playerName);
nameCorrect = Console.ReadLine();
message = "Apologies, what is the correct name?";
}
while (nameCorrect == "No");
Upvotes: 2
Reputation: 1201
If you store your prompt in a string variable, you can change it after the first time. Just make sure you put that change in a conditional so you don't end up reconstructing a new immutable string every time through the loop.
string nameCorrect;
string playerName;
string namePrompt = "What is your name?";
do
{
Console.WriteLine(namePrompt);
if (namePrompt == "What is your name?") namePrompt = "Apologies, what is the correct name?"
playerName = Console.ReadLine();
Console.WriteLine("Is {0} correct?", playerName);
nameCorrect = Console.ReadLine();
} while(nameCorrect == "No");
if(nameCorrect == "Yes")
{
Console.WriteLine("Great, lets move on.");
}
Console.ReadKey();
Upvotes: 0