Your code does not make sense:
getchar( )
- > read 1 and only one, for many more than pulses.
while( ... )
- > I guess you want to wait until a correct character is pressed, but, since you have left the getchar( )
out of the loop, either it enters an infinite loop, or it does not do anything (if the character is 'a', 'b' , or 'c').
switch( ) { ... }
- > It's the only thing that seems to be fine.
You already have enough clues to go pulling ...
EDITO
I am ashamed to admit that I did not see the do
. I'll be getting old.
Having that do
in mind, the thing changes a lot. Now if I see him I mean this, and your question, if I understood it correctly.
Do you want me to recognize the pressing of the 1st key, without having to press ENTER?
Short answer, for a change: you can not . The I / O functions wait until you press ENTER. There's nothing to do (in Linux, you could put the terminal in raw mode, but that's another story).
EDIT 2
With the new explanation of the user, the thing has become clearer. What you want to do is read a string and, if you have more than 1 character , indicate the error.
You can not use use getchar
. Use fgets
or scanf
.
If you use the one, you will need a buffer to store the string. There is
thrice questions about arrays and arrays on this site,
anyone can serve you.
You have to get the length of the string read. If the length is > 1
, it is a user error and treat it as such.
Otherwise, check the first character of the string.