1

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++.
Ophelia
  • 549
  • 1
  • 5
  • 26
  • Windows or Linux? Do you want single character input or whole line input? – cup May 23 '15 at 05:05
  • Linux. I dont care much, as long it works as intended and not very hard. Preferably single character, more preferably that without even pushing enter, but I think Inow I go for anything just to fix it:D – Ophelia May 23 '15 at 05:08
  • Again, who put a minus on my post? I can't see why, just leave a comment too! – Ophelia May 23 '15 at 05:09
  • Instead of handling one character, why not input the whole number with *cin >>* – cup May 23 '15 at 07:46

1 Answers1

1

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');
dev_ankit
  • 516
  • 4
  • 8
  • oh, it is part of some microsoft libraries, isn't it? I use gcc compiler, so it is not and option – Ophelia May 23 '15 at 04:57
  • include `#include ` for getch() – dev_ankit May 23 '15 at 04:58
  • hm, it is not a standart library? I need to download it somewhere? my gcc don't recognize it – Ophelia May 23 '15 at 05:07
  • 2
    Neither `curses.h` nor `conio.h` are standard libraries. Try to use `std::cin` form the standard `` and [validate the input](http://stackoverflow.com/questions/2075898/good-input-validation-loop-using-cin-c). The latter won't be able to do it without pushing ENTER, but you should prefer it, since it is the portable way of doing things. If you need more sophisticated input, then you should indeed consider using a third party library like [libncurses](http://www.gnu.org/software/ncurses/). – vsoftco May 23 '15 at 05:14
  • @Ophelia does the new answer helps? – dev_ankit May 23 '15 at 05:33
  • Yep, definitly better, the best idea I got so far. At least I dont have to fall low and accept string as input and use compare:) Thanks a lot. – Ophelia May 23 '15 at 05:46