![]() |
Lost in a loop
I'm trying to incorporate a little more error checking in my program. I have this menu that lists four possible choices:
1. Addition 2. Subtraction 3. Multiplication 4. Division I want to add some code so that if the user enters a choice(a number)that's not on the menu, a message will display and the program will keep prompting him until he enters either a 1, 2, 3, or 4. But what I got isn't working. Even when I enter a correct choice, the program continues to loop instead of going on with the rest of the program. Don't know where I'm going wrong. Here is what I got: :
cout << "\nChoose which operation to perform: \n\n"; |
Where are the while's curley braces. Also you are using a binary or(|) you need a logical or(||).
See http://www.cplusplus.com/doc/tutorial/operators.html. |
You might consider "choice < 1 || choice > 4". I'm pretty sure we've discussed this before: always check your input for success. What happens if I enter 'z'? You'll never pick up a correct entry, even if I subsequently make one, that's what.
|
DaWei has a point. You should check for errors on the iostream. You can do this by calling the method cin.bad() to determine if there is a problem with the input.
|
Actually, a good return from cin.bad () does not mean there was no failure. Check the documentation for cin.good (), cin.fail (), and cin.bad ().
|
My program still isn't working correctly. I did add braces for the while, and I did change the binary OR to the logical OR:
:
while (response != 1 || response != 2 || response != 3 || response != 4)No matter what I enter, whether its a 1, 2, 3, 4(all correct choices) the program says "Invalid choice..." |
I even tried adding parentheses around everything but it still doesn't work.
|
I think I'm using the wrong operator. I got to use Logical AND(&&).
|
:
while (response != 1 || response != 2 || response != 3 || response != 4)Quote:
See, you say you want it to execute the while code block if response does not equal one, or if response does not equal two (or three, or four). Let's say response did equal one; it therefore could not be two, meaning the second test would be true. Combining a true value with either a false or true will yield true. In fact, by using what is called 'short-circuit evaluation', the third and fourth cases will never be evaluated, and the second will only be evaluated sometimes. Short-circuit evaluation is where the program can determine the total result of a combined expression that uses logical or/and. For example, if you have A || B || C, and A is true, there is no need to evaluate B or C (since regardless of their values, the net result will be true). Likewise, for A && B && C, if A is false, the result must be false, so there's no sense evaluating B or C. |
Your code is going to be redundant, anyway, because you're going to have to test again to decide whether to add, subtract, etc. Why don't you use a switch? The default case would then represent improper input.
|
| All times are GMT -5. The time now is 2:36 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC