-3

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;
    }
}
5
  • 2
    Hint: you're only only calling Console.ReadLine() after you've told the user you're asking for their name again... they haven't entered it once! Commented Sep 3, 2024 at 17:19
  • 1
    What exactly are you asking? Commented Sep 3, 2024 at 17:22
  • @David I'm asking how do I 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. Commented Sep 3, 2024 at 17:27
  • 3
    @KennyG: You're asking us to do your homework for you, which isn't something we do here. Please see: Open letter to students with homework problems If your current code isn't working as expected, this is a good opportunity for you to start familiarizing yourself with using a debugger. When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? Commented Sep 3, 2024 at 17:28
  • @David I understand. I did not want anyone to solve my homework for me since I actually want to learn. I guess I've could've asked it in a different way but I'm new to all this so you'll have to excuse me. Commented Sep 3, 2024 at 17:32

3 Answers 3

2

What you have missed:

  1. You don't prompt a user for a name before loops starts, therefore, you have nothing to compare further inputs against.

  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

1 Comment

Thanks a lot! I guess I should've asked it a different way since I didn't want anyone to do my homework for me though I appreciate your help. I'm new to all this and I need to get better at asking the right question.
0

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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.