Problem with gets() and scanf in C.

Started by
4 comments, last by nuclearfission 10 years, 3 months ago

Hello,

I am having a problem when I use scanf and then gets().

Why do these two functions confilct together when used one after the other? Maybe I do something wrong?

for example:

char A[20],B[20];
int number;
printf("Number: ");
scanf("%d",&number);
printf("A: ");
gets(A);
printf("B: ");
gets(B);
the compiler just ignores gets(A)...
How can I solve this problem?
thanks in advance tongue.png

Failure is not an option...

Advertisement

scanf("%d") tells the program to get the standard input and load what's there as an integer and then leave the rest in the buffer. That means if your input is something like "9\n" then it just grabs the 9 but leaves the \n in the buffer. So when gets() comes along it sees the \n in the buffer and then decides it's done because gets() reads standard input until it sees a \n. One way to handle this is to use gets() to read the line that contains the number into a character array and then use sscanf() to parse that character array. Usual caveats about buffers sizes and error handling with C I/O apply.

Note that scanf family is very nearly impossible to use safely, and should never be used in production code. EVER.

Consider the input: 999999999999999

Then consider this line from the language standard:

If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is unde?ned.


Even if you limited it by something like %6d it is still possible to abuse the library in ways that fail horribly. Also if you ever face non-numeric input like "abcd" or "12a3" you will leave content in the stream. Then you must do extra work to remove the data from the stream and won't have any way to detect garbage data.


If you must read numbers that way, read a full string at once using fgets() and check for errors. If that succeeded use strtol() to convert it to a number and again check for errors such as the resulting number being only part of the string, out-of-range values, or not-numeric data. Then you probably want to double check that the user didn't accidentally use an octal notation (starting with zero) or maybe you want to keep that behavior.

Yeah, scanf is bad. sscanf or fscanf though is good if you (yourself) write the string in the format you like before parsing it, it's one of my favourite functions. It makes parsing easy.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Ok guys thanks for explaining me where the problem is. :P

Failure is not an option...

^What the moderators said above^

AND

One alternative solution to fix this problem is to use fflush to flush out the buffer. This is what my C/C++ professor made us use to learn C. However, this is non-standard; use it at your own discretion.


char A[20],B[20];
int number;

printf("Number: ");
scanf("%d",&number);

fflush (stdin); // flush out buffer

printf("A: ");
gets(A);
printf("B: ");
gets(B);

This topic is closed to new replies.

Advertisement