Input in C. Scanf before gets. Problem?

Up vote 4 down vote favorite 2 share g+ share fb share tw.

I'm pretty new to C, and I have a problem with inputing data to the program. My code: #include #include #include int main(void) { int a; char b20; printf("Input your ID: "); scanf("%d", &a); printf("Input your name: "); gets(b); printf("---------"); printf("Name: %s", b); system("pause"); return 0; } It allows to input ID, but it just skips the rest of the input. If I change the order like this: printf("Input your name: "); gets(b); printf("Input your ID: "); scanf("%d", &a); It will work.

Although, I CANNOT change order and I need it just as-is. Can someone help me? Maybe I need to use some other functions.

Thanks! C input scanf gets link|improve this question asked Mar 2 '10 at 20:30Dmitri996 100% accept rate.

Gets(3): "The gets() function cannot be used securely. Because of its lack of bounds checking, and the inability for the calling program to reliably determine the length of the next incoming line, the use of this function enables malicious users to arbitrarily change a running program's func- tionality through a buffer overflow attack. It is strongly suggested that the fgets() function be used in all cases.

(See the FSA. )" Don't use it. – Bertrand Marron Mar 2 '10 at 20:39 1 More briefly: If you use gets, flying rabid attack ocelots will rip out your eyesockets.

So don't. – Tyler McHenry Mar 2 '10 at 20:45 1 scanf is evil - c-faq.com/stdio/scanfprobs.html – jschmier Mar 2 '10 at 22:07.

Try: scanf("%d\n", &a); gets only reads the '\n' that scanf leaves in. Also, you should use fgets not gets: cplusplus.com/reference/clibrary/cstdio/... to avoid possible buffer overflows. Edit: if the above doesn't work, try: ... scanf("%d", &a); getc(stdin); ...

Hm. I've tried scanf("%d\n", &a); but it doesn't seems to be working. After I input ID, I just don't see program doing anything else.

– Dmitri Mar 2 '10 at 20:37 Wow! Thanks! That worked flawlessly!

Thanks for advice about fgets. Will surely use it! Thank you!

– Dmitri Mar 2 '10 at 20:47.

Scanf doesn't consume the newline and is thus a natural enemy of fgets. Don't put them together without a good hack. Both of these options will work: // Option 1 - eat the newline scanf("%d", &a); getchar(); // reads the newline character // Option 2 - use fgets, then scan what was read char tmp50; fgets(tmp, 50, stdin); sscanf(tmp, "%d", &a); // note that you might have read too many characters at this point and // must interprete them, too.

Awesome! Thank you very much! – Dmitri Mar 2 '10 at 20:51 Also, using scanf("%d%*c", &a) should work.

The %*c term causes scanf to read in one character (the newline) but the asterisk causes the value to be discarded. This will make scanf eat the newline without requiring a separate function call. – bta Mar 2 '10 at 20:57 You present both options as if they had equal footing, but scanf is just so hard to use properly that it's much better to avoid using it entirely (see the comp.lang.

C FAQ link). Just go with option 2. – jamesdlin Mar 3 '10 at 3:23.

Scanf will not consume \n so it will be taken by the gets which follows the scanf. Flush the input stream after scanf like this. #include #include int main(void) { int a; char b20; printf("Input your ID: "); scanf("%d", &a); fflush(stdin); printf("Input your name: "); gets(b); printf("---------"); printf("Name: %s", b); system("pause"); return 0; }.

1 Both fflush(stdin) and fflush(NULL), in some C libraries, will flush stdout and stderr, but this is completely unportable! – Kalmi Nov 29 '10 at 22:08.

You might find the answers to this question helpful stackoverflow.com/questions/2360183/fget....

Include #include #include int main(void) { int a; char b20; printf("Input your ID: "); scanf("%d", &a); getchar(); printf("Input your name: "); gets(b); printf("---------"); printf("Name: %s", b); return 0; } Note: If you use the scanf first and the fgets second, it will give problem only. It will not read the second character for the gets function. If you press enter, after give the input for scanf, that enter character will be consider as a input f or fgets.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions