C# How to Loop User Input Until the Datatype of the Input Is Correct

C# How to loop user input until the datatype of the input is correct?

while (!int.TryParse(Console.ReadLine(), out mynum))
Console.WriteLine("Try again");

edit:

public void setX() {
Console.Write("Enter a value for X (int): ");
while (!int.TryParse(Console.ReadLine(), out x))
Console.Write("The value must be of integer type, try again: ");
}

Try this. I personally prefer to use while, but do .. while is also valid solution. The thing is that I don't really want to print error message before any input. However while has also problem with more complicated input that can't be pushed into one line. It really depends on what exactly you need. In some cases I'd even recommend to use goto even tho some people would probably track me down and slap me with a fish because of it.

How do I make a loop that prompts the user to enter data every time they enter an invalid data type?

You should use a do or while loop to keep repeating the prompt until a valid double is entered. You should also consider adding some form of exit keywords. Like if they enter "exit, quit, q" etc.. Have it terminate the app instead of loop back around. However being a console app, ctrl + c will close it regardless of what it's doing (it's the kill command) but not everyone knows that.

bool repeat = true;
var dblMilesMon = (double)0;
do
{
Console.WriteLine("Enter value for Monday : ");
var strMilesMon = Console.ReadLine();
if (!double.TryParse(strMilesMon, out dblMilesMon))
Console.WriteLine("You entered an invalid number - please enter a numeric value.")
else
repeat = false;
}while (repeat);

//do something with dblMilesMon

How can I loop and check for correct user input in C#?

It is not efficient to check twice a thing, you are checking user input in if-statements and again in the while-condition.

The simplest way is to set up a bool type variable to indicate whether user has entered the correct response or not.

bool flag = false;
do{
//your code
}
while(!flag)

and add this in every if-statement:

flag = true;

indicating that the user has inputed the correct value.

Re-prompt if input is less than or equal to 0

It should works:

int n;
do
{
Console.Write("Choose a pyramid height: ");
n = Int32.Parse(Console.ReadLine());
if ( n <= 0) Console.WriteLine("Value must be greater than 0.");
}
while ( n <= 0 );

C# How to loop user input until the datatype of the input is correct?

while (!int.TryParse(Console.ReadLine(), out mynum))
Console.WriteLine("Try again");

edit:

public void setX() {
Console.Write("Enter a value for X (int): ");
while (!int.TryParse(Console.ReadLine(), out x))
Console.Write("The value must be of integer type, try again: ");
}

Try this. I personally prefer to use while, but do .. while is also valid solution. The thing is that I don't really want to print error message before any input. However while has also problem with more complicated input that can't be pushed into one line. It really depends on what exactly you need. In some cases I'd even recommend to use goto even tho some people would probably track me down and slap me with a fish because of it.



Related Topics



Leave a reply



Submit