Reputation: 23
I'm a beginner to programming and have this task I want to complete.
Task at hand:
Write a program where the user is allowed to enter their name, then (in a while loop) the program prompts the user to type enter your name again. If the user fills in the same name as the first time, the program ends (break), otherwise the program continues to ask.
I tried to use this code that I wrote:
Console.Write("Enter your name: ");
string name = "";
while (name != "")
{
Console.Write("Enter your name again: ");
name = Console.ReadLine();
if (name != "")
{
break;
}
}
Upvotes: -3
Views: 118
Reputation: 37470
What you have missed:
You don't prompt a user for a name before loops starts, therefore, you have nothing to compare further inputs against.
Your loop condition checks if entered name is not empty, that's it, which is totally different then your requirement "stop when user enters name as on first attempt".
Correcting these and using your code we come to:
Console.Write("Enter your name: ");
// Store first input
string name = Console.ReadLine();
while (name != "")
{
Console.Write("Enter your name again: ");
// Read input and compare it against name that was first entered.
// If it's the same, break out of the loop.
if (name == Console.ReadLine())
{
break;
}
}
However, this can be improved. First of all, while
checks boolean condition, so we don't need to do if...break
, and move the condition to while
:
Console.Write("Enter your name: ");
// Store first input
string name = Console.ReadLine();
while (name != Console.ReadLine())
{
Console.Write("Enter your name again: ");
}
This is better, but we would need to enter name second time without prompt Enter your name again:
- but here we can extract method like this:
string PromptForName(string prompt)
{
Console.WriteLine(prompt);
return Console.ReadLine();
}
and then use it like this
// Store first input
string name = PromptForName("Enter your name: ");
while (name != PromptForName("Enter your name again: ")) { }
I used Console.WriteLine
to write prompt and then feed new line.
Upvotes: 2
Reputation: 416121
Remove the whole if()
block, including the contents, change the !=
to ==
in the while()
statement, and initialize the variable with a read:
Console.Write("Enter your name: ");
string name = Console.ReadLine();
while (name == "")
{
Console.Write("Enter your name again: ");
name = Console.ReadLine();
}
But I would also tend to have a method like this (with similar methods added for int, double, date, etc as needed):
// negative attempts means keep trying forever
public string PromptString(string prompt, string errorReprompt, int attempts = -1, bool throwOnAttemptsExpire = false)
{
string input = null;
while(string.IsNullOrWhiteSpace(input) && attempts-- != 0)
{
Console.Write(prompt);
input = Console.ReadLine();
prompt = errorReprompt;
}
if (string.IsNullOrWhiteSpace(input))
{
if (throwOnAttemptsExpire) throw new Exception($"Attempts expired for prompt: {prompt}");
return null;
}
return input;
}
And then call it like this:
string name = PromptString("Enter your name: ", "Enter your name again: ");
Later, I might also added a predicate delegate argument for validation, to replace string.IsNullOrWhiteSpace()
Upvotes: 0
Reputation: 27
One problem that I found with the given code is; a: they do not enter their name originally; and b: you do not have a way to store their old input. In order to complete your task I would recommend using this code
Console.WriteLine("enter your name");
string name = "";
string checkname = "";
name = Console.ReadLine();
while (name != "")
{
Console.WriteLine("re enter your name");
checkname = Console.ReadLine();
if (checkname == name)
{
break;
}
}
Hope this helps!
Upvotes: 0