2

I wrote a console program which is getting some spesific character inputs from user to perform operations. Program asks for an input, user types "i" for insert or "r" for remove etc.. The problem is when user types more than one letter, the program behaves weird.

char selection;
bool finish = false;
while (!finish){
    print_menu();
    cin >> selection;
    finish = perform_operation(selection);
}

This is how i get the user input. And i want to get just the first character of the user input and ignore the rest of them, and behave so on. How can i do it?

Note: I'm not allowed to use "string" class, but i can use every str functions!

Thanks for help!

Another case:

What if i would like to get spesific amount of characters? For example: the user inputs a name which is 10 characters but i want to get just the first 5 characters. What should i do?

Community
  • 1
  • 1
Tolga Şen
  • 289
  • 5
  • 14
  • Do you want to get first "non white" character from input and ignore the following character till the `\n` ? – PiotrNycz Oct 12 '15 at 11:41

4 Answers4

6

If you want to read one character, read one character.

Don't have a loop that reads as many characters as possible.

char selection;
print_menu();
if (cin >> selection)
   finish = perform_operation(selection);
else
   throw std::runtime_error("Bad input!");

I've added basic error checking to your I/O operation for good measure.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • This is a program which is getting user input as long as a person doesn't exit from the program. So i definitely have to use a while loop. Usin if block is not an option for this case – Tolga Şen Oct 12 '15 at 12:51
  • @TolgaŞen: Either you want to read just one character, or you want to read multiple characters. Which is it? – Lightness Races in Orbit Oct 12 '15 at 13:28
  • I'd like to read exact the same amount of characters which i give to the function. It might be just one or multiple also – Tolga Şen Oct 12 '15 at 19:34
1

for getting just one character you can use also this:

char selection;
cin >> selection; // input: abcd
cin.clear();
fflush(stdin);
printf("you entered the character: %c", selection); //selection='a'
// and nothing remained in input stream. you can safely `cin>>` again :]

You can apply this code to your existing one.


In order to get a specific number of charter you can use below sample:

char* TakeNCharacterFromInput(int _count)
{
    char *input = new char[_count+2];
    std::cin.getline(input, _count+1);
    cin.clear();
    fflush(stdin);
    return input;
}

int main(int args, char* argv[]) {
    char *in1 = TakeNCharacterFromInput(5);//input:123456789 => in1:12345
    char *in2 = TakeNCharacterFromInput(2);//input:123456789 => in2:12
    char *in3 = TakeNCharacterFromInput(1);//input:123456789 => in3:1
    delete[] in1;
    delete[] in2;
    delete[] in3;
}
Emadpres
  • 3,466
  • 2
  • 29
  • 44
  • 2
    Um your suggested change doesn't actually change anything. – Lightness Races in Orbit Oct 12 '15 at 11:21
  • Yeah edited code works fine but i forgot to ask, i really need a function that takes an input with given spesific amount of characters. For example i need user to enter a name but i just want to get first 20 characters of that name and ignore the rest. How can i do that? – Tolga Şen Oct 12 '15 at 13:00
  • cin.clear() and fflush(stdin) is not working for me, I still can get the others inputs from cin. I'm Using Ubuntu. – Pedro77 Feb 19 '20 at 11:44
1

Ignore everything except the first character until the next line break:

char selection;
bool finish = false;
while (!finish){
    print_menu();
    cin >> selection;
    finish = perform_operation(selection);
    cin.ignore(10000, '\n'); // Totally arbitrary large number.
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

skip always reprinting the menu

print_menu();

then use a function that waits for ENTER:

Several ways to do so, here are some possible one-line approaches:

  1. Use getch() (need #include ).

  2. Use getchar() (expected for Enter, need #include ).

  3. Use cin.get() (expected for Enter, need #include ).

  4. Use system("pause") (need #include ).

see also this thread C++ wait for user input

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34