Sign in to follow this  
shadowstep00

Problem with gets() and scanf in C.

Recommended Posts

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
Edited by shadowstep00

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

^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);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this