Kerry G
Kerry G

Reputation: 947

Looping a process in C#

I'm familiar with loops, yet looping a process has left me bewildered:

If the user enters a non-integer, I would like the question 'your age' to be prompted again until the user enters an integer.

 Console.WriteLine("Your age:");
 string line = Console.ReadLine();
 if (!int.TryParse(line, out age))
 {
     Console.WriteLine("{0} is not an integer", line);

 }

Upvotes: 0

Views: 157

Answers (5)

Pradip
Pradip

Reputation: 1537

I have used this approach. I dont know whether this will decrease the performance or not but I find it cool to use regex. Let me know if this works out for ya

Add this to the TOP

using System.Text.RegularExpressions;

Then use the following :

            bool bEnteredNumberNotValid = true;
            while (bEnteredNumberNotValid)
            {
                Console.WriteLine("Your age:");
                string sAge = Console.ReadLine();

                string regString = "(^[0-9]+$)"; //REGEX FOR ONLY NUMBERS

                Regex regVal = new Regex(regString, RegexOptions.IgnoreCase | RegexOptions.Singleline); //REGEX ENGINE
                Match matVal = regVal.Match(sAge); //REGEX MATCH WITH THE INPUT
                if (!matVal.Success) // IF THERE IS NO MATCH, SHOW THE BELOW
                {
                    Console.WriteLine("{0} is not an integer", sAge);
                }
                else // ELSE SET bEnteredNumberNotValid FALSE AND GET OUT.
                {
                    bEnteredNumberNotValid = false;
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadLine();
                }
            }

OUTPUT!

Click here to see the output of above program

Hope this helps.

Upvotes: 2

cuongle
cuongle

Reputation: 75306

Try this, it will make "Your age: " repeat until you have correct input:

int age;
while(true)
{
    Console.WriteLine("Your age:");
    string line = Console.ReadLine();

    if (!int.TryParse(line, out age))
       Console.WriteLine("{0} is not an integer", line);

    else break;
}

Upvotes: 6

Mr_Green
Mr_Green

Reputation: 41832

I know only Recursive Function to achieve this but it is not recommended because it is error prone and makes the program overly complicated.

In class

 string line;
 int age = 0;

In Main

 Console.WriteLine("Your age:");   
 line  = Console.ReadLine();  
 checkFunction();

Declare a method

public int checkFunction()
{
  if (!int.TryParse(line, out age))
  {
    Console.WriteLine("{0} is not an integer", line);
    line = Console.ReadLine();
    return checkFunction();
  }
  else
  {
    return age;
  }
}

Upvotes: 0

Gabber
Gabber

Reputation: 5452

If I understand well your question, why don't you just do

Console.WriteLine("Your age:");
string line = Console.ReadLine();
while (!int.TryParse(line, out age))
{
    Console.WriteLine("{0} is not an integer", line);
    Console.WriteLine("Your age:");
    line = Console.ReadLine();
}

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

Try

int age;

Console.WriteLine("Your age:");
string line = Console.ReadLine();
while (!int.TryParse(line, out age))
{
    Console.WriteLine("{0} is not an integer", line);
    Console.WriteLine("Your age:");
    line = Console.ReadLine();
}

I'm not sure what you mean by looping a process. You are looping around getting user input and attempting to parse that input.

You can principally accomplish that using while, do, for, or (gulp! don't do it!) goto.

Upvotes: 7

Related Questions