0

I would like to know what to put in the if statement brackets to tell the program, if x or y equals a double, it can break out and continue carrying out the rest of my code.

Any suggestions?

while (true)
{                    
    Console.Write("I need to pour this much from this one: ");

    string thisOne = Console.ReadLine();
    Double.TryParse(thisOne, out x);

    if ( /* Here I want to put "x is a number/double*/ )
    {
        break;
    }

}

while (true)
{
    Console.Write("I need to pour this much from that one: ");

    string thatOne = Console.ReadLine();
    Double.TryParse(thatOne, out y);

    if (/* Here I want to put "y is a number/double*/)
    {
        break;
    }
}
1

5 Answers 5

4

TryParse returns a boolean to say whether the parse was successful

if (Double.TryParse(thatOne, out y))
{
    break;
}

From documentation

A return value indicates whether the conversion succeeded or failed.

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

Comments

2

Double.TryParse returns a boolean, perfect fit for your if statement

if (Double.TryParse(thatOne, out y)) {
    break;
}

Comments

2

You have a misconception about TryParse(). You want to check if x is a double. somewhere above in your code, you did not post it here there is probably a line like double x = 0;. You defined x and y already as double. You want to check if your input which is string can be parsed to double:

The shorthand version is this:

if (Double.TryParse(thatOne, out x))
{
    break;
}

This can also be written as:

bool isThisOneDouble = Double.TryParse(thisOne, out x);

if (isThisOneDouble)
{
    break;
}

If you really want to check if a variable is of certain type without trying to parse it, try it like this:

double x = 3;
bool isXdouble = x.GetType() == typeof(double); 

or

double x = 3;
if(x.GetType() == typeof(double)) 
{
   // do something
}

Comments

0

According to the documentation, TryParse returns true if parsing succeeded so just put your tryparse into your if statement.

Comments

0

Control your loop with a bool, set the bool false when your condition is met...

bool running = true;
while (running)
{                    
    Console.Write("I need to pour this much from this one: ");

    string thisOne = Console.ReadLine();

    if (Double.TryParse(thisOne, out y))
    {
         running = false
    }
}

3 Comments

Seems like an unnecessary step? This also requires the loop to perform a full iteration, the OP might want to break out early.
It also doesn't answer the question of what goes into the if statement.
Just edited to include, missed that from the original question. The last step is a break so not adding extra execution.

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.