How do I prevent program from running BOTH an IF and ELSE statement with C++?

You need to implement if-else if- else statements:

if (hun > 100) 
{
    std::cout << "Pick a number LESS than 100. ";
}
else if (hun > 50)
{
    std::cout << "Your number is greater than 50. ";
}
else if (hun < 50)
{
    std::cout << "Your number is less than 50. ";
}
else
{
    std::cout << "Your number is equal to 50. ";
}

The reason why the original code didn’t work is that the else was only linked to the last if. So if a number satisfied one of the earlier if statements but not the last if it would go to both the if it satisfied and the else as the last if was not satisfied.

Additionally you must reorder it so that the more extreme cases are first. Otherwise if hun is more that one hundred but you have the condition hun > 50 then it will go to that if-else and then skip the rest.

Leave a Comment