943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 665
  • C++ RSS
Aug 23rd, 2009
0

Loop Continuously or output invalid choice

Expand Post »
Hello

I have a program that prints a menu & the user is prompted to input a choice(1,2 or 3) if they input anything else, the output should be "Invalid Choice".

My Problem
is that if a user inputs anything other than 1,2 or 3, then the program crashes, instead of doing what it should do "Invalid Choice".

What I am essentially trying to do is make the menu loop continuously unless option 3 is input (option 3 = quit).

Any advice would be really helpful. Such as should I uuse a whole new method of making the menu loop?

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. // function prototypes
  7. void printMenu();
  8. void decision(int action);
  9.  
  10. int main()
  11. {
  12. int option;
  13.  
  14. // loop to display menu, get user choice and perform required action
  15.  
  16. while(option != 3) {
  17. printMenu();
  18. cin >> option; // if I type anything other than 1,2,3 the program crashes
  19. decision(option);
  20. }
  21.  
  22.  
  23. //system("pause");
  24. return 0;
  25. }
  26.  
  27. void printMenu()
  28. {
  29.  
  30. cout << "\tCrossword Helper " << endl << endl;
  31. cout << " 1 : Check if word is in dictionary " << endl;
  32. cout << " 2 : Find words in dictionary which match pattern " << endl;
  33. cout << " 3 : Quit " << endl << flush;
  34.  
  35. }
  36.  
  37. void decision(int action) { // Are these variable & function names acceptable?
  38.  
  39. string word;
  40.  
  41. switch (action) {
  42. case 1:
  43. cout << "\nEnter word: \n" << flush;
  44. cin >> word;
  45. to_lower(word);
  46. if (binarySearch(words,word,0,nWord)) {
  47. cout << word << " is in the dictionary\n";
  48. }
  49. else
  50. cout << word << " is NOT in the dictionary\n";
  51. break;
  52. case 2:
  53. cout << "\nEnter word pattern with ? for missing letters\n" << flush;
  54. cin >> word;
  55. to_lower(word);
  56. search(words,nWord,word);
  57. break;
  58. case 3:
  59. // I dont think I need to put anything here because break will stop the function then main will return 0
  60. //system("pause");
  61. //return 0;
  62. break;
  63. default:
  64. cout << "Invalid Choice";
  65. break;
  66. }
  67. }
Last edited by gretty; Aug 23rd, 2009 at 1:52 am.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

Well i compiled the program and it seems to work just fine.

Are you sure, that the program crashes if INput is >4?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

The problem seems to be if I input strings or characters, it crashes
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

Try checking cin.fail(). If it is true, something went wrong with the input operation.

Good Luck!
Reputation Points: 82
Solved Threads: 1
Newbie Poster
wheel is offline Offline
16 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

i ll use an if code to determine the input is char or integer, i think the program crashes because option is integer
Reputation Points: 17
Solved Threads: 2
Light Poster
gkaykck is offline Offline
40 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

Try to use char variable of choice. Then your code will be like this
cpp Syntax (Toggle Plain Text)
  1. //declaration
  2. void decision(char action);
  3. //implementation
  4. void decision(char action) { // Are these variable & function names acceptable?
  5.  
  6. string word;
  7.  
  8. switch (action) {
  9. case '1':
  10. cout << "\nEnter word: \n" << flush;
  11. cin >> word;
  12. to_lower(word);
  13. if (binarySearch(words,word,0,nWord)) {
  14. cout << word << " is in the dictionary\n";
  15. }
  16. else
  17. cout << word << " is NOT in the dictionary\n";
  18. break;
  19. case '2':
  20. cout << "\nEnter word pattern with ? for missing letters\n" << flush;
  21. cin >> word;
  22. to_lower(word);
  23. search(words,nWord,word);
  24. break;
  25. case '3':
  26. // I dont think I need to put anything here because break will stop the function then main will return 0
  27. //system("pause");
  28. //return 0;
  29. break;
  30. default:
  31. cout << "Invalid Choice";
  32. break;
  33. }
  34. }
And main
cpp Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. char option;
  4.  
  5. // loop to display menu, get user choice and perform required action
  6.  
  7. while(option != '3') {
  8. printMenu();
  9. cin >> option; // if I type anything other than 1,2,3 the program works correctly
  10. decision(option);
  11. }
  12.  
  13.  
  14. //system("pause");
  15. return 0;
  16. }

P.S. Also you can use getchar() or cin.get(), or _getche() mothod to get the char choice.
I hope it will help you.
Last edited by Protuberance; Aug 23rd, 2009 at 5:45 am.
Reputation Points: 78
Solved Threads: 17
Junior Poster in Training
Protuberance is offline Offline
88 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

Click to Expand / Collapse  Quote originally posted by gretty ...
Hello

I have a program that prints a menu & the user is prompted to input a choice(1,2 or 3) if they input anything else, the output should be "Invalid Choice".

My Problem
is that if a user inputs anything other than 1,2 or 3, then the program crashes, instead of doing what it should do "Invalid Choice".

What I am essentially trying to do is make the menu loop continuously unless option 3 is input (option 3 = quit).

Any advice would be really helpful. Such as should I uuse a whole new method of making the menu loop?
Did you initialize option ? you seem to be commenting //system("pause"); .Are you really using a standard compiler?

>>The problem seems to be if I input strings or characters, it crashes

First get whatever input in a temporary string and then use strtol to get your option

c++ Syntax (Toggle Plain Text)
  1. int option = 0 ;
  2. string option_string;
  3.  
  4. while(option != 3) {
  5. printMenu();
  6. char *end;
  7. getline(cin, option_string);
  8. option = strtol(option_string.c_str(), &end, 10);
  9. cout << "You enterd option" << option;
  10. decision(option);
  11. }
strtol
Last edited by zalezog; Aug 23rd, 2009 at 6:35 am.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Aug 23rd, 2009
0

Re: Loop Continuously or output invalid choice

use stringstream. Convert string to numerics. To avoid all the integer
hassle.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

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: why can the reference data member be assigned in constant member function?
Next Thread in C++ Forum Timeline: strcmp





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


Follow us on Twitter


© 2011 DaniWeb® LLC