Code Executing Even When Conditions Aren't Satisfied?

Mar 16, 2014 at 7:36pm
In the program I'm writing, I begin it by getting two pieces of input (both are type "double") using cout and cin statements. If the first double is < 0 OR the second double is < 0 OR the second double is > 100, then an error message is printed. If not, then a series of if/else statements are executed based on the numbers the user input.

When I type in a random string of letters for the first double, the program automatically skips over the second input prompt and prints the error message. But when I type in a random string of letters for the second double, the program executes normally and assigns a value of 0 to the second double (which, if it is of any consequence, is what the next "else if" statement needs to execute. That is to say, the second double == 0).

My question is this: why will the program allow a string of random letters/numbers for the second double when it has been declared as a double? I'm not at liberty to show the code, but as I stated above, the program automatically jumps to the next "else if" statement as if the second double was == 0.

Not sure if this can be easily resolved without access to my code, but thanks for any attempts.
Mar 16, 2014 at 7:43pm
My question is this: why will the program allow a string of random letters/numbers for the second double
It is not. Your second double is unchanged and loop set to failure state, moreover failed inpud stays in input stream and will cause all future reads to double fail as well unless manually cleared (That is why it skips second variable input). If you did not set variable values before, they contains undefined values which can be anything.
Mar 16, 2014 at 7:50pm
Could you elaborate on how to fix the second double so it will operate as I intend? I'm sorry for my lack of understanding.
Mar 16, 2014 at 7:53pm
Do not enter random strings? Check for failure status after each input operation then clear input stream and make user reenter value if needed?
Topic archived. No new replies allowed.