Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
switch statement over performing with char variable in c++
#include<iostream>

using namespace std;

void fun() {
   while(1) {
      char choice;
      cout<<"(D)isplay, (E)xit"<<endl;
start:
      cout<<">> ";
      cin>>choice;
      switch(choice) {
         case 'd':
         case 'D':
            cout<<"hello world"<<endl;
            break;
         case 'e':
         case 'E':
            return;
         default:
            cout<<"INVALID!"<<endl;
            goto start;
      }
   }
}
int main() {
   system("clear");
   fun();

   return 0;
}
            

This is a simple c++ program. I am faciing following problem:

(D)isplay, (E)xit
>> mnop
INVALID!                                                  
>> INVALID!                                               
>> INVALID!
>> INVALID!
>>

The symbol '>>' prompt user to enter a character and with that it give apppropriate result but if entered a string it behaves something like this. I want to know why this happens and how to stop it :)

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Try calling cin.clear() after getting your value, and possibly also cin.ignore(INT_MAX). This will clear the buffer for you.

over 4 years ago · Santiago Trujillo Report

0

Your code does what you told it to but not more: cin>>choice; reads a single character. When the user types more than a single character then the other characters are left in the stream and will be read by the next call.

If you want to handle input of more than a single character properly then you need to extract more than single character from the stream. For example:

 while (true) {
     std::string input;
     std::cin >> input;
     if (input.size() > 1 || input.size() == 0) {
         std::cout << "enter only a single character\n";
     } else {
         choice = input[0];
         break;
     }
} 

Alternatively you could check if there are more characters left in the stream after reading one and/or simply discard them (via ignore).

over 4 years ago · Santiago Trujillo Report

0

cin >> choice , you are reading only one char. Remaining chars remain in buffer and read when you again call cin >> choice. Try string asdf then you will see output.

You can flush std in or simply read all remaining chars using cin.read();

basically clear input buffer by reading all chars.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!