Hey, working on this project and thought I was finished with it but today I let
ID: 3883447 • Letter: H
Question
Hey,
working on this project and thought I was finished with it but today I let my girlfriend mess with it and she entered a letter (j) instead of a number on the menu and got an infinite loop of my error message. Here is the menu code:
do {
//display menu
cout << "----------------------------------------------------------- ";
cout << "County Auditor Database Search Menu: ";
cout << endl;
cout << "Enter 1 = All Taxes and Properties " << "Enter 2 = Due taxes (Ascending) " << "Enter 3 = Display largest balance " << "Enter 4 = Address look-up " << "Enter 5 = Close County Auditor Search Program ";
cout << "----------------------------------------------------------- ";
cout << "Selection: ";
cin >> choice;
cout << endl;
//validate user's choice
while (choice < 1 || choice > 5) {
//if (!(cin >> choice)){ // tried to use this also
cout << "Enter a valid selection: ";
cin >> choice;
}
//process user's choice
if (choice != 5) {
//respond with user's menu selection
switch (choice) {
case 1:
displayAddressesAmtDue(address, amtDue, count);
break;
case 2:
sortDisplayAddressTaxDue(address, amtDue, count);
break;
case 3:
displayLargestDelTaxBill(address, amtDue, count);
break;
case 4:
lookUpTaxes(address, amtDue, count);
}
}
} while (choice != 5);
}
Explanation / Answer
I have modified the code.in this given code a break statement was missing
int choice;
do {
//display menu
cout << "----------------------------------------------------------- ";
cout << "County Auditor Database Search Menu: ";
cout << endl;
cout << "Enter 1 = All Taxes and Properties " << "Enter 2 = Due taxes (Ascending) " << "Enter 3 = Display largest balance " << "Enter 4 = Address look-up " << "Enter 5 = Close County Auditor Search Program ";
cout << "----------------------------------------------------------- ";
cout << "Selection: ";
cin >> choice;
cout << endl;
//validate user's choice
while (choice < 1 || choice > 5) {
//if (!(cin >> choice)){ // tried to use this also
cout << "Enter a valid selection: ";
cin >> choice;
}
//process user's choice
if (choice != 5) {
//respond with user's menu selection
switch (choice) {
case 1:
displayAddressesAmtDue(address, amtDue, count);
break;
case 2:
sortDisplayAddressTaxDue(address, amtDue, count);
break;
case 3:
displayLargestDelTaxBill(address, amtDue, count);
break;
case 4:
lookUpTaxes(address, amtDue, count);
break;
}
}
} while (choice != 5);
}