943,670 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 570
  • C++ RSS
May 28th, 2008
0

Menu Flaw - Query.

Expand Post »
Basically this Menu works however, when I enter in a string the default does catch it but then the Menu repeats the error so in essence isnt full proof. Would the way around this be to check the 1st entry by the user to see if its an integer, if so progress to the do while if not give an initial error message till they enter an integer? Or is there a wiser way around this annoying flaw? Thanks.

C++ Syntax (Toggle Plain Text)
  1. do { // Do While Loop that incorporates Menu and List Functions
  2. system("cls"); //clears previous entries & redisplays the Menu per selection
  3. // Displays Menu Screen for user interaction of List
  4. cout << "**********Menu*************"<< endl;
  5. cout << "* 1. Populate List *"<< endl;
  6. cout << "* 2. Sort list *"<< endl;
  7. cout << "* 3. Search List *"<< endl;
  8. cout << "* 4. Display List *"<< endl;
  9. cout << "* 0. Exit *"<< endl;
  10. cout << "***************************"<< endl;
  11. cout << " "<< endl;
  12. cout << "Please select your option "<< endl;
  13. cin >> n;
  14. cout << " "<< endl;
  15.  
  16. switch (n)
  17. {
  18. case 1 :
  19. disp.populate();
  20. cout << "List populated."<< endl;
  21. system("Pause");
  22. break;
  23. case 2 :
  24. disp.sort();
  25. system("Pause");
  26. break;
  27. case 3 :
  28. cout << "Please enter the value you wish to search from the List"<< endl;
  29. cin >> p;
  30. system("Pause");
  31. break;
  32. case 4 :
  33. disp.toString();
  34. system("Pause");
  35. break;
  36. default:
  37. if (n !=0)
  38. {
  39. cout << "Enter a number relating to the Options shown!!!" << endl;
  40. system("Pause");
  41. }
  42. break;
  43. }
  44. }
  45. while (n != 0); // Exit Condition
  46. system("Pause");
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007
May 28th, 2008
1

Re: Menu Flaw - Query.

Your loop encompasses both the menu and the selector, as:
C++ Syntax (Toggle Plain Text)
  1. do {
  2. display_menu();
  3. select_option();
  4. while (!done);
So if the option is bad, the menu gets displayed again anyway.

What you'd probably like instead is something that catches itself. You can do it several ways, but another loop will probably do just fine.
C++ Syntax (Toggle Plain Text)
  1. do {
  2. display_menu();
  3. do {
  4. select_option();
  5. while (invalid_option);
  6. while (!done)

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 29th, 2008
1

Re: Menu Flaw - Query.

Hey Just use the return Statement to Exit.

and in the switch statments Add the following. So the program returns a value and ends.
C++ Syntax (Toggle Plain Text)
  1. case 0:
  2.  
  3. return 0;
  4.  
  5. break;
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
May 29th, 2008
0

Re: Menu Flaw - Query.

Cheers guys - Duoas and Sky Diploma.

Ill attempt both suggestions!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Run.[it] is offline Offline
57 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Help with program...can't get array to accept a struct
Next Thread in C++ Forum Timeline: Image. How to get an array that works properly?





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


Follow us on Twitter


© 2011 DaniWeb® LLC