0
votes

I have an assignment in my C# class that I am confused on. Here is the assignment below. My question is how to implement the carriage return? It seems like using /r resets to the beginning of the line... I was going to use string.empty in a while loop, but I can't tell by the instructions if there is a way to get the desired result by using the carriage return. I feel like I am looking way to into this. Any thoughts?

Let's take a moment to get our program to do something. Modify your program to:
● Prompt the user for their name
● Store the input string into a variable named str01.
● Output a greeting to the console using the string stored in str01. (ex. "Hello, Ethel!")
● Keep prompting the user to enter a name.
● If they enter an empty string (a simple carriage return), then drop out of the loop with an appropriate message (ex. "See you next time!").

class App01
{
  static void Main(string[] args)
  {
    string str01;
    for(;;)
    {
      Console.Write ("Enter your name -");
      str01 = Console.ReadLine();
      Console.WriteLine("Hello '{0}'", str01);
    }
  }
}
1
Use a while loop that should do the same actions, until the str01 variable is eqaul to a carriage return. - Ryan Wilson
place a break point after your ReadLine(). Run and press Enter. Look at the value of str01. - MikeH

1 Answers

1
votes

From the docs:

The ReadLine method reads a line from the standard input stream..

A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or the value of the Environment.NewLine property. The returned string does not contain the terminating character(s).

If no characters are entered in and we just hit the carriage return, we just get the carriage return an empty string. A line is the sequence of characters up to the terminating character. In this case the carriage return.

So to address your concern, the desired result by the assignment definition is to just check if the line read into str01 is empty and display See you next time! if it is.