Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:44:09+00:00 2026-06-09T22:44:09+00:00

If user key in letter like a , it will result in endless loop,

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:44:10+00:00Added an answer on June 9, 2026 at 10:44 pm

    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:

    int choice = 0;
    while (!(cin >> choice) && (choice < 1 || choice > 4)) {
        cout << "Invalid selection - Please input 1 to 3 only.\n";
        // reset error flags
        cin.clear();
        // throw away garbage input
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
        // the above two statements prevent infinite loop due to
        // bad stream state
    }
    
    // proceed to switch statement
    

    numeric_limits template is located in <limits> header.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement key-press functionality which will remove a div when the user
Is it somehow possible to access a user's public key files (located in ~/.ssh
I have 3 tables - user, service and ratings. The user's primary key is
The object user has a foreign key relationship to address. Is there a difference
I'm translating the following key in activity.fr.yml user.list.link: '{1}et %count% autre|]1,Inf[voir les %count% autres'
I have a User table which has a PrivilegeId foreign key points to a
I need to know when the user presses the escape key when watching an
I am trying to get the user defined global hot key for my application.
Hello I am doing a program that will notice when a specific key is
Possible Duplicate: Run javascript function when user finishes typing instead of on key up?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.