Search code examples
c++getchar

C++ getchar dont work as it should


I have this code, which gives users suggestion to select one of the options in my menu:

char c;
    do {
        switch(c=getchar()){
                 case '1':
                      cout << "Print" << endl;
                      break;
                 case '2':
                      cout << "Search" << endl;
                      break;
                 case '3':
                      cout << "Goodbye!" << endl;
                      break;
                 default:
                     cout << "I have this symbol now: " << c << endl;
                      break;
                }
      } while (c != '3');

So, it suppose to to read the character and put us in one of the three options. And it does. But only after I push enter, and well, I can live with that, but it also accepts these string as a valid options:

  • dfds2kflds, fdsf3, fds1lkfd

What the hell? I want it to accept only character like this:

  • 1, 2, 3 How can I fix it? I am a noob at c++.

Solution

  • Use either getche()or getch(). If you do sometthing like this

    c=getch();
    switch(c=getch()){
                 case '1':
                      cout<<c;
                      cout << "Print" << endl;
                      break;
                 case '2':
                      cout<<c;
                      cout << "Search" << endl;
                      break;
                 case '3':
                      cout<<c;
                      cout << "Goodbye!" << endl;
                      break;
                 default:
                      break;
                }  
    

    You will not see any other character except 1,2 and 3 on the screen

    ** EDIT **
    If conio.h is not available and you can try this: (discard rest of characters in line)

    char c;
    do {
        switch(c=getchar()){
                 case '1':
                      cout << "Print" << endl;
                      break;
                 case '2':
                      cout << "Search" << endl;
                      break;
                 case '3':
                      cout << "Goodbye!" << endl;
                      break;
                 default:
                     cout << "I have this symbol now: " << c << endl;
                      break;
                }
                while((c=getchar())!='\n'); //ignore rest of the line
      } while (c != '3');
    

    or discard inputs where more than 1 characters are there

    char c;
    do {
        c=getchar();
        if(getchar()!='\n'){ //check if next character is newline
            while(getchar()!='\n'); //if not discard rest of the line
            cout<<"error"<<endl;
            c=0; // for case in which the first character in input is 3 like 3dfdf the loop will end unless you change c to something else like 0
            continue;
        }
            switch(c){
                 case '1':
                      cout << "Print" << endl;
                      break;
                 case '2':
                      cout << "Search" << endl;
                      break;
                 case '3':
                      cout << "Goodbye!" << endl;
                      break;
                 default:
                     cout << "I have this symbol now: " << c << endl;
                      break;
                }
      } while (c != '3');