Loop Continuously or output invalid choice

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 150
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Loop Continuously or output invalid choice

 
0
  #1
Aug 23rd, 2009
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Loop Continuously or output invalid choice

 
0
  #2
Aug 23rd, 2009
Well i compiled the program and it seems to work just fine.

Are you sure, that the program crashes if INput is >4?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 150
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Loop Continuously or output invalid choice

 
0
  #3
Aug 23rd, 2009
The problem seems to be if I input strings or characters, it crashes
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 16
Reputation: wheel will become famous soon enough wheel will become famous soon enough 
Solved Threads: 1
wheel wheel is offline Offline
Newbie Poster

Re: Loop Continuously or output invalid choice

 
0
  #4
Aug 23rd, 2009
Try checking cin.fail(). If it is true, something went wrong with the input operation.

Good Luck!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: gkaykck is an unknown quantity at this point 
Solved Threads: 2
gkaykck's Avatar
gkaykck gkaykck is offline Offline
Light Poster

Re: Loop Continuously or output invalid choice

 
0
  #5
Aug 23rd, 2009
i ll use an if code to determine the input is char or integer, i think the program crashes because option is integer
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 66
Reputation: Protuberance will become famous soon enough Protuberance will become famous soon enough 
Solved Threads: 14
Protuberance's Avatar
Protuberance Protuberance is offline Offline
Junior Poster in Training

Re: Loop Continuously or output invalid choice

 
0
  #6
Aug 23rd, 2009
Try to use char variable of choice. Then your code will be like this
  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
  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.
"If a problem can be decided - it is not needed to worry, and if to decide it is impossible - worrying is useless." (с)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 46
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Loop Continuously or output invalid choice

 
0
  #7
Aug 23rd, 2009
Originally Posted by gretty View 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?
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: Loop Continuously or output invalid choice

 
0
  #8
Aug 23rd, 2009
use stringstream. Convert string to numerics. To avoid all the integer
hassle.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 389 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC