944,187 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6767
  • C++ RSS
Nov 1st, 2006
0

Problem with do-while loop

Expand Post »
Hi all! THis is the program I am supposed to write:
Modify the program so it displays a menu allowing the user to select an addition, subtraction, multiplication, or division problem. The final selection on the menu should let the user quit the program. After the user has finished the math problem, the program should display the menu again. This process is repeated until the user chooses to quit the program.
  • Input Validation: If the user selects an item not on the menu, display an error message and display the menu again.
Additional notes to pc4:
  • Do not implement the subtraction, multiplication, or division. Instead, provide a message to the effect that the module is not yet ready.
  • Use a switch statement to process menu choices.
  • Use a 'do while' loop, as shown below.
  • Here is pseudocode of what should go in the do loop for this exercise:
  • do { 1. addition
  • 2. subtraction
  • 3. etc
  • 5. quit Which would you like?
  • switch (choice)
  • case 1: the whole program you already wrote
  • case 2: not available etc.
  • case 5: Thank you and goodbye!
  • default: got to be kidding, try again! }
  • while( choice != 5 ); 'do while' loops and 'switch' work great for menus. The screen capture for this assignment should demonstrate a 'Not Available' and 'Exit' choice
The problem I am having is that it won't compile and I"m not sure why. This is the code I have so far does anyone know what I'm doing wrong?
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int choice; // menu choice
  9.  
  10. cout << "This program written by Katy Pedro cs102 Online." << endl;
  11.  
  12. do
  13. {
  14. cout << " MENU " << endl;
  15. cout << " 1. Addition\n";
  16. cout << " 2. Subtraction\n";
  17. cout << " 3. Multiplication\n";
  18. cout << " 4. Division\n";
  19. cout << " 5. Quit the program\n";
  20. cout << "_____________________\n";
  21.  
  22.  
  23. while (choice < 1 || choice > 5)
  24. {
  25. cout << "Enter your selection: ";
  26. cin >> choice;
  27. }
  28. if (choice !=5)
  29. {
  30. cout << "I'm sorry but that is not available!" << endl;
  31.  
  32. switch (choice)
  33. {
  34. case 1: cout << "Please make a different choice, this program is not available.\n";
  35. break;
  36. case 2: cout << "Please make a different choice, this program is not available.\n";
  37. break;
  38. case 3: cout << "Please make a different choice, this program is not available.\n";
  39. break;
  40. case 4: cout << "Please make a different choice, this program is not available.\n";
  41. break;
  42. case 5: "Thank you for making the right choice! GOODBYE for now!\n";
  43. break;
  44. default: cout << "Are you serious? Try Again!!!\n";
  45. }
  46.  
  47. system("PAUSE");
  48. return 0;
  49.  
  50. }

THanks in advance any help is greatly appreciated!!!:cheesy:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newbie2c++ is offline Offline
19 posts
since Oct 2006
Nov 1st, 2006
0

Re: Problem with do-while loop

  1. if (choice !=5)
  2.  
  3. {
  4.  
  5. cout << "I'm sorry but that is not available!" << endl;
You forgot to add a closing brace.

  1. system("PAUSE");
  2.  
  3. return 0;
You never ended the do loop; do loops require a closing brace and a while statement to follow.

And lastly, you never ended the main function! A closing brace is all that's needed.
Last edited by John A; Nov 1st, 2006 at 9:35 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 2nd, 2006
0

Re: Problem with do-while loop

Here's something that you might find useful. Because the description of the problem isnt really the clearest it could be, I made this little program up to help you understand what should happen. Its using a functino called DoMenu which displays the menu and gets input from there.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int DoMenu();
  6.  
  7. int main()
  8. {
  9. int choice;
  10. do
  11. {
  12. choice = DoMenu();
  13.  
  14. switch (choice)
  15. {
  16. case 1: cout << "addition\n"; break;
  17. case 2: cout << "subtraction\n"; break;
  18. case 3: cout << "multiplication\n"; break;
  19. case 4: cout << "division\n"; break;
  20. case 5: cout << "quit\n"; break;
  21. default : cout << "Error input\n";
  22. }
  23.  
  24. } while (choice != 5);
  25.  
  26. return 0;
  27. }
  28.  
  29. int DoMenu()
  30. {
  31. int MenuChoice;
  32. cout << " MENU\n";
  33. cout << " 1. Addition\n";
  34. cout << " 2. Subtraction\n";
  35. cout << " 3. Multiplication\n";
  36. cout << " 4. Division\n";
  37. cout << " 5. Quit the program\n";
  38. cout << "_____________________\n";
  39. cin >> MenuChoice;
  40. return MenuChoice;
  41. }
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006
Oct 11th, 2009
-3
Re: Problem with do-while loop
Create an application that will input any integer values and display it in reverse using looping in visual basic 2008. please answer thanks^^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leika is offline Offline
1 posts
since Oct 2009
Oct 12th, 2009
0
Re: Problem with do-while loop
A. You are responding to a three year old post
B. You are hijacking that thread with a new problem
C. You are asking for a solution to a problem you have not attempted to solve on your own
D. This is a C++ forum, you're asking for a VB program

Read the sticky threads at the beginning of the forums, and chose the correct forum for your problem. You might then get some help.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with doubles and floats, apparently
Next Thread in C++ Forum Timeline: Infix Parentheses Balancing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC