| | |
Loop Continuously or output invalid choice
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
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?
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)
#include <cstdlib> #include <iostream> using namespace std; // function prototypes void printMenu(); void decision(int action); int main() { int option; // loop to display menu, get user choice and perform required action while(option != 3) { printMenu(); cin >> option; // if I type anything other than 1,2,3 the program crashes decision(option); } //system("pause"); return 0; } void printMenu() { cout << "\tCrossword Helper " << endl << endl; cout << " 1 : Check if word is in dictionary " << endl; cout << " 2 : Find words in dictionary which match pattern " << endl; cout << " 3 : Quit " << endl << flush; } void decision(int action) { // Are these variable & function names acceptable? string word; switch (action) { case 1: cout << "\nEnter word: \n" << flush; cin >> word; to_lower(word); if (binarySearch(words,word,0,nWord)) { cout << word << " is in the dictionary\n"; } else cout << word << " is NOT in the dictionary\n"; break; case 2: cout << "\nEnter word pattern with ? for missing letters\n" << flush; cin >> word; to_lower(word); search(words,nWord,word); break; case 3: // I dont think I need to put anything here because break will stop the function then main will return 0 //system("pause"); //return 0; break; default: cout << "Invalid Choice"; break; } }
Last edited by gretty; Aug 23rd, 2009 at 1:52 am.
Well i compiled the program and it seems to work just fine.
Are you sure, that the program crashes if INput is >4?
Are you sure, that the program crashes if INput is >4?
Try to use char variable of choice. Then your code will be like this
And main
P.S. Also you can use getchar() or cin.get(), or _getche() mothod to get the char choice.
I hope it will help you.
cpp Syntax (Toggle Plain Text)
//declaration void decision(char action); //implementation void decision(char action) { // Are these variable & function names acceptable? string word; switch (action) { case '1': cout << "\nEnter word: \n" << flush; cin >> word; to_lower(word); if (binarySearch(words,word,0,nWord)) { cout << word << " is in the dictionary\n"; } else cout << word << " is NOT in the dictionary\n"; break; case '2': cout << "\nEnter word pattern with ? for missing letters\n" << flush; cin >> word; to_lower(word); search(words,nWord,word); break; case '3': // I dont think I need to put anything here because break will stop the function then main will return 0 //system("pause"); //return 0; break; default: cout << "Invalid Choice"; break; } }
cpp Syntax (Toggle Plain Text)
int main() { char option; // loop to display menu, get user choice and perform required action while(option != '3') { printMenu(); cin >> option; // if I type anything other than 1,2,3 the program works correctly decision(option); } //system("pause"); return 0; }
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." (с)
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
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?
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)
int option = 0 ; string option_string; while(option != 3) { printMenu(); char *end; getline(cin, option_string); option = strtol(option_string.c_str(), &end, 10); cout << "You enterd option" << option; decision(option); }
Last edited by zalezog; Aug 23rd, 2009 at 6:35 am.
use stringstream. Convert string to numerics. To avoid all the integer
hassle.
hassle.
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan]
3) What is the 123456789 prime numer?![]() |
Similar Threads
- Restaurant Menu Program (C++)
- questions with case statements (C++)
- Javascript help. Loop once! (JavaScript / DHTML / AJAX)
- c++ letter coding (C++)
- exiting loop with switch (C++)
- C Program where gets(), getchar(), are not working. (C)
- C++ help with tic tac toe program (C++)
- C - Getting double output? (C)
- Writing a loop statement that produces desired output (Java)
Other Threads in the C++ Forum
- Previous Thread: why can the reference data member be assigned in constant member function?
- Next Thread: strcmp
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






