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 promptsprompt and then feed new line.