Home > Net >  Try-parse C# infinite amount of times for user to enter a number?
Try-parse C# infinite amount of times for user to enter a number?

Time:11-26

I'm trying to learn how to use TryParse (am very new to programming so I'm sorry if it's a stupid question!). I would like to do so that the user can try again how many times they want to and I guess I could do it with a while loop and try-catch? but I would like to learn ho to do it with Try-parse if posible. I've tried to make a try parse and a while loop with an if else statement but it didn't work, the user got to try again but it saved the first thing that was entered and not the second.. I've tried to search around and I've found many different ways on how to use try-parse but not in a way that the user can try an infinite amount of times. Right now I've made it so that the user can try 3 (with the code below - tell me if I should insert more of the code here!) but I would appreciate if someone have a tip on how I could do so the user get infinite times to try!

Thank you in advance!

// Ask user to enter age, save information, clear program view
Console.WriteLine("Enter "   name   "`s age:");

int age;
string stringAge;
bool parseOkay;
stringAge = Console.ReadLine();
parseOkay = int.TryParse(stringAge, out age);

if (parseOkay == true)
{
}
else
{
    Console.WriteLine("Invalid numeric input.\nPlease Write in only one number:");
    stringAge = Console.ReadLine();
    parseOkay = int.TryParse(stringAge, out age);
    if (parseOkay == true)
    {
    }
    else
    {
        Console.WriteLine("Invalid numeric input.\nPlease Write in only one number:");
        stringAge = Console.ReadLine();
        parseOkay = int.TryParse(stringAge, out age);
        if (parseOkay == true)
        {
        }
        else
        {
            Console.WriteLine("Third attempt, invalid numeric input.\nPlease press enter to return to the menu and then try again.");
            Console.ReadLine();
            return;
        }
    }
}

CodePudding user response:

If I were you, I would do some research on while loops.

Using a while loop, you can not only simplify this, but actually get rid of a good amount of your variables too.

For example, the following code would keep asking the user to re-enter a number until they provide a valid one.

int age;
while(!int.TryParse(Console.ReadLine(), out age)){
    Console.WriteLine("Please enter a valid number.");
}

Console.WriteLine("You are "   age   " years old!");

Here's a .NET Fiddle with this code that you can play around with.

Side note about your if statements: If statements are checking a condition (aka boolean or bool), so doing if(x == true) is redundant. You can simply do if(x).

On top of this, all your if statements are empty, and your code is in the else block. If you need to check if a condition is false, you can use the logical negation operator (aka logical not operator), which is simply an exclamation point before the condition. So, doing

if(!x){
   //Code here
}

would be the same as doing

if(x){
}
else{
    //Code here
}

CodePudding user response:

Since tryParse returns a bool, you could use that function inside a while loop. Here is an example:

int age;

Console.WriteLine("Enter your age: ");
while (!int.TryParse(Console.ReadLine(), out age))
{
    Console.WriteLine("Invalid numeric input.\nPlease Write in only one number:");
}
  • Related