I am working on a menu driven program to output an adjacency matrix for a homework assignment. When I put a character in as my input after I call any of the other 4 cases my loop runs infinitely and I can't figure out why.
This happens regardless to if I have case 5 active.
But if I enter a character as the first input then it closes properly as intended.
I stepped through the debugger and it seems that if a character is entered it takes that input to be 4 and never takes another input so it keeps printing the array over and over.
Can anyone explain what is wrong with this function? I only have the function here because the entire program is roughly 300 lines not counting comments. But through some testing I've narrowed my bug down to this specific function the others do what they are meant to.
void menu(char graph[][8])
{
bool run = true;
while (run == true)
{
int menuChoice;
cout << "Welcome to the menu" << endl;
cout << "Pick one of the following" << endl;
cout << "1. add connection" << endl;
cout << "2. delete connection " << endl;
cout << "3. show total number of connections " << endl;
cout << "4. show matrix " << endl;
cout << " 5. to exit" << endl;
cout << "Selection : ";
cin >> menuChoice;
switch (menuChoice)
{
case 1: addConnection(graph);
break;
case 2: deleteConnection(graph);
break;
case 3: showConnection(graph);
break;
case 4: showMatrix(graph);
break;
/*case 5:
cout << "Exiting ...\n";
run = false;
break;*/
default:
cout << "Improper input " << endl; // for some reason this flies into infinite when a character is entered.
cout << "Exiting ...\n";
run = false;
break;
}
}
}