ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Try using getchar().
Actually the i/p should fail during 2,4,... since \n supplied after entering the required character will remain in the buffer and will be accepted as the next character.
Anyway, u also try fflush(stdin) before scanf statements.
how are you testing it? scanf reads the enter key as an input, so if you test by running & then typing a character, followed by an enter key, you're actually giving it two inputs. try this:
Originally posted by naihe2010 But how can I get 1 char every time ?
Well, the code you posted seems a little "generic" so it's hard to tell what you are trying to accomplish but to read a char 8 times, why not try something like this:
Code:
for(int i=0; i<8; i++)
{
scanf("%c", &ch);
while(ch=='\n') // If the char was a linebreak, keep scanning until you get one that isn't
scanf("%c", &ch);
// Do something with the char here
}
I'm not really sure what you are after but hopefully this might help. If you only want to read numbers of letters, ctype.h contains a function isalnum() that will tell you if a character is either a number of letter. You could use that in the while loop above to skip everything else, including white space.
In my whole program, I wrote lots of scanf().
But some scanf() doesn't work well. That is, they get the '\n' from the last scanf() .
It 's so disgusting.
I want only get a char, but doesn't influence the next scanf() to get a char narmally.
Originally posted by naihe2010
It 's so disgusting.
Write your own scanf routine if you don't like it. I don't use scanf functions for user input because of this 'unpredictable' behaviour. I always use fgets(buffer,size,stdin) and do the conversions myself. That way, the behaviour is always predictable (the newline is included in the buffer).
PS when you analyse the behaviour, it's probably not as unpredictable as it looks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.