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?
#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;
}
}