dimanche 7 avril 2019

User Input Error Message Not Outputting Correctly

I'm trying to ask the user to enter a number for the 3 questions. I want an error message to come up if the user enters something that isn't 0 or above it. Also, it must be a number. If these are entered, the error comes up and you get another try.

Currently, for the feet, the error message comes up and the question comes back. For the inches and pounds though, when you enter something that isn't a number, the error message comes up, but the question doesn't. It goes straight to the next question. I'm not sure where I made the mistake.

double _heightVal = 0;
double _inchesVal = 0;
double _poundsVal = 0;
bool _userNotDone = true;

do
{
    //Ask the user for height
    Console.Write("Please enter the first part of your height in feet:");
    string _height = Console.ReadLine();
    if (!double.TryParse(_height, out _heightVal))
    {
        Console.WriteLine("Error. You must enter a number. Please try again.");
    }
    else if (_heightVal < 0)
    {
        Console.WriteLine("Error.Number must be greater than 0. Please try again.");
    }
    else
    {
        _userNotDone = false;
    }
}
while (_userNotDone == true || _heightVal < 0);

do
{ 
    //Ask the user for inches
    Console.Write("Please enter the second part of your height in inches:");
    string _inches = Console.ReadLine();
    if (!double.TryParse(_inches, out _inchesVal))
    {
        Console.WriteLine("Error. You must enter a number. Please try again.");
    }
    else if (_inchesVal < 0)
    {
        Console.WriteLine("Error.Number must be greater than 0. Please try again.");
    }
    else
    {
        _userNotDone = false;
    }
}
while (_userNotDone == true || _inchesVal < 0) ;

do
{
    //Ask the user for inches
    Console.Write("Please enter your weight in pounds:");
    string _pounds = Console.ReadLine();
    if (!double.TryParse(_pounds, out _poundsVal))
    {
        Console.WriteLine("Error. You must enter a number. Please try again.");
    }
    else if (_poundsVal < 0)
    {
        Console.WriteLine("Error.Number must be greater than 0. Please try again.");
    }
    else
    {
        _userNotDone = false;
    }
}
while (_userNotDone == true || _poundsVal < 0);

Aucun commentaire:

Enregistrer un commentaire