If user key in letter like “a” , it will result in endless loop, the default: doesnt work.
How do i do exception handling so its will output error message instead of endless loop.
Thanks!
Below is my code:
done=false;
do
{
cout << "Please select the department: " << endl;
cout << "1. Admin " << endl;
cout << "2. HR " << endl;
cout << "3. Normal " << endl;
cout << "4. Back to Main Menu " << endl;
cout << "Selection: ";
cin >> choice;
switch (choice) {
case 1:
department_selection = "admin";
done=true;
break;
case 2:
department_selection = "hr";
done=true;
break;
case 3:
department_selection = "normal";
done=true;
break;
case 4:
selection = "hr_menu";
done=true;
break;
default:
cout << "Invalid selection - Please input 1 to 3 only.";
done=false;
}
}while(done!=true);
The problem isn’t your switch statement but the fact that you don’t check if input operation actually succeeded. Always use input operations in some boolean context:
numeric_limitstemplate is located in<limits>header.