Check if user input is double and greater than zero?

In C #, I am trying to enter a user number. Then I want to check that

  • They entered a string that can be converted to double and
  • They entered a value greater than zero

The method I originally created was

string inValue; double outcome; Console.WriteLine("Enter amount: "); inValue = Console.ReadLine(); while (double.TryParse(inValue, out outcome) == false) { Console.WriteLine("Initial value must be of the type double"); Console.WriteLine("\nPlease enter the number again: "); inValue = Console.ReadLine(); } outcome = double.Parse(inValue); while (outcome < 0) { Console.WriteLine("Initial value must be of at least a value of zero"); Console.WriteLine("\nPlease enter the number again: "); inValue = Console.ReadLine(); outcome = double.Parse(inValue); } return outcome; 

The problem was that if the user enters the word "-10" and then "f", an exception will occur. This is because the program will go past the first check (which checks double) for the value -10, but then when β€œf” is entered, it throws an exception if the second test is given.

I believe that the solution is to create a while statement that writes an error statement when either the value cannot be converted to double, or the value is converted to double and is below zero. What I don’t know how to do is convert the value to double and then evaluate to more than zero in the while statement.

+6
source share
4 answers

You're on the right track β€” you need to have one while loop that gets input, and then tries to check. One way to do this is to create a boolean that keeps track of whether this value is valid, and then use this as a condition for the loop:

 double outcome = 0; bool valid = false; while (!valid) { Console.WriteLine("Enter amount: "); string inValue = Console.ReadLine(); if (double.TryParse(inValue, out outcome) == false) { Console.WriteLine("Initial value must be of the type double"); Console.WriteLine("\nPlease enter the number again: "); } else if (outcome < 0) { Console.WriteLine("Initial value must be of at least a value of zero"); Console.WriteLine("\nPlease enter the number again: "); } else { valid = true; } } return outcome; 

You can also include both conditions in a while statement, but this approach allows you to provide a different message depending on which conditions failed.

+7
source

you can either these two conditions in your first while loop

sort of

 while (!double.TryParse(inValue, out outcome) || outcome < 0) { ... } 

some explanation: double.TryParse will change the value of the result if it is successful, therefore, if it could parse, then! TryParse will be evaluated as false, so we go to the second part and evaluate the result <0.

+6
source

I would go with that decision. Just noting that you don't need double.Parse(Value) after doing double.TryParse(value, out outcome) , the out parameter will populate this variable if TryParse is true.

You can copy and paste the code below LinqPad and play with it. But it does what you need.

 void Main() { var result = DoWork(); Console.WriteLine(result); } public double DoWork() { string inValue; double outcome; Console.WriteLine("Enter amount: "); inValue = Console.ReadLine(); while (!double.TryParse(inValue, out outcome) || outcome <= 0) { Console.WriteLine("Initial value must be of the type double and greater than 0"); Console.WriteLine("\nPlease enter the number again: "); inValue = Console.ReadLine(); } return outcome; } 
+2
source

after a little regrouping, this is the working code! but I think you sometimes need to use if instead

  string inValue = ""; double outcome = -1; Console.WriteLine("Enter amount: "); while (outcome < 0){ inValue = Console.ReadLine(); if (double.TryParse(inValue, out outcome) == false) { Console.WriteLine("Initial value must be of the type double"); Console.WriteLine("\nPlease enter the number again: "); continue; } if (outcome>=0) {continue;} Console.WriteLine("Initial value must be of at least a value of zero"); Console.WriteLine("\nPlease enter the number again: "); } return outcome; 
0
source

All Articles