How to make program stop when empty string is entered?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Santek Moreli

    How to make program stop when empty string is entered?

    I am creating a simple program using a couple methods that will ask the user to input data (type int) until an empty string is entered, after which the output will be displayed.

    I need to figure out how to make the input stop when an empty string is entered and have the output displayed after that.

    I am using a while loop, which needs to read:

    Code:
    int number;
    
    while (number is not equal to an empty string)
    {
    statement;
    }
    There also needs to be an if statement inside of the while loop that reads:

    Code:
    if (number is not equal to an empty string)
    {
    statement;
    }
    
    else
    break;
    Any help would be appreciated, thanks!
    Last edited by Curtis Rutland; Oct 12 '10, 10:15 PM. Reason: Added CODE tags. Please use [CODE] tags when posting code.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Well, the simplest thing to do is just check:
    Code:
    if([B]userInput[/B] == string.Empty)
    Replace userInput with the name of the variable holding your user input.

    Then, to break out of a loop, simply use the break statement:
    Code:
    if(condition)
      break;
    There's really no need to do both a break and a while, but you could always do something like this:

    Code:
    string input = Console.ReadLine();
    while(input != string.Empty)
    {
      //do something with the input
      input = Console.ReadLine();
    }

    Comment

    Working...