2

Right now, I converted all 3 strings into an int, but I'm having trouble converting each string using the Double.TryParse method. I want to use that method instead of int.

I tried using this type of code if (Double.TryParse(value, out number)), but I'm not to sure exactly if this is right.

//Ask the user for height
Console.Write("Please enter the first part of your height in feet:");
string _height = Console.ReadLine();
int _heightVal = Int32.Parse(_height);

//Ask the user for inches
Console.Write("Please enter the second part of your height in inches:");
string _inches = Console.ReadLine();
int _inchesVal = Int32.Parse(_inches);

//Ask the user for pounds
Console.Write("Please enter your weight in pounds:");
string _pounds = Console.ReadLine();
int _poundsVal = Int32.Parse(_pounds);
3
  • 2
    I don't see where you;re using Double.TryParse. Commented Apr 4, 2019 at 16:48
  • tryparse should be wrapped in an if where you somehow deal with the failure. learn.microsoft.com/en-us/dotnet/api/… Commented Apr 4, 2019 at 16:49
  • Double seems like a bad choice for feet and inches. Maybe for weight, but if it is fractional perhaps use decimal. Commented Apr 4, 2019 at 16:53

2 Answers 2

11
double heightVal = 0;
double.TryParse(_height, out heightVal); 

heightVal will have the value of the Parse from _height if Parsing is successful, otherwise it will have the it's previous value (0 in here)

TryParse() returns a boolean indicating if the Parsing was successful that you can use it like:

bool success = double.TryParse(_height, out heightVal); 

or

if(double.TryParse(_height, out heightVal))
{
     //Parse was successful and heightVal contains the new value
     // and you can use it in here
}

Fail Example:

double defaultValue = 0;
string str = "abc"
bool success = double.TryParse(str, defaultValue );

Output:

defaultValue = 0

success = false

Success Example:

double defaultValue = 0;
string str = "123"
bool success = double.TryParse(str, defaultValue );

Output:

defaultValue = 123

success = true

Sign up to request clarification or add additional context in comments.

2 Comments

Might want to add an example that utilizes the returned boolean.
@JonathonChase I'm doing it ;)
0

i think you just trying to force use enter the correct value you can use wile loop like this

 double userHeight = 0.0;
        while (true)
        { 
            //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 double height))
            {
                userHeight = height;
                break;
            }

        }

then apply to all your questions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.