Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 11th, 2007, 2:54 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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";
	cout << "1. Addition\n";
	cout << "2. Subtraction\n";
	cout << "3. Multiplication\n";
	cout << "4. Division\n\n";

	cout << "Enter which operation to perform: ";
	cin >> response;

	if (response != 1 | response != 2 | response != 3 | response != 4)
	{
		cout << "Invalid choice. Please enter a valid choice: ";
	    cin >> response;
	}

	while (response != 1 | response != 2 | response != 3 | response != 4)
    
		if (response != 1 | response != 2 | response != 3 | response != 4)
		{
			cout << "Invalid choice. Please enter a valid choice: ";
			cin >> response;
		}
357mag is offline   Reply With Quote
Old Jul 11th, 2007, 3:08 AM   #2
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
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.
__________________
Geeks may not be cool now but in the long run they prosper.
teencoder is offline   Reply With Quote
Old Jul 11th, 2007, 7:57 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 11th, 2007, 10:58 AM   #4
francoissoft
Newbie
 
Join Date: Jul 2007
Location: Ohio
Posts: 19
Rep Power: 0 francoissoft is on a distinguished road
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.
francoissoft is offline   Reply With Quote
Old Jul 11th, 2007, 11:50 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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 ().
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 12th, 2007, 12:40 AM   #6
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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)
	{
    
		if (response != 1 || response != 2 || response != 3 || response != 4)
		{
			cout << "Invalid choice. Please enter a valid choice: ";
			cin >> response;
		}
	}

No matter what I enter, whether its a 1, 2, 3, 4(all correct choices) the program says "Invalid choice..."
357mag is offline   Reply With Quote
Old Jul 12th, 2007, 12:46 AM   #7
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I even tried adding parentheses around everything but it still doesn't work.
357mag is offline   Reply With Quote
Old Jul 12th, 2007, 12:56 AM   #8
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I think I'm using the wrong operator. I got to use Logical AND(&&).
357mag is offline   Reply With Quote
Old Jul 12th, 2007, 2:04 AM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
while (response != 1 || response != 2 || response != 3 || response != 4)
{
  if (response != 1 || response != 2 || response != 3 || response != 4)
  {
    cout << "Invalid choice. Please enter a valid choice: ";
    cin >> response;
  }
}
See how the test conditions for your while and if statements (red) are the same? That's one problem; it's redundant.
Quote:
Originally Posted by 357mag
I think I'm using the wrong operator. I got to use Logical AND(&&).
This is your second problem. Using the or operator as you've done (whether logical or bitwise doesn't matter) will always yield a result of true. This means the code in blue will always be executed.

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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jul 12th, 2007, 6:34 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of index incorrect after loop aznluvsmc C 13 Nov 6th, 2005 9:47 PM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 12:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 12:27 PM
Completely lost, help needed bliznags C++ 2 Mar 19th, 2005 4:44 PM
Timing loop problems badbasser98 C++ 11 Mar 10th, 2005 8:30 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC