Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problem with gets() and scanf in C.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 shadowstep00   Members   -  Reputation: 540

Like
0Likes
Like

Posted 25 December 2013 - 01:44 PM

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, 25 December 2013 - 01:44 PM.

Failure is not an option...


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9670

Like
3Likes
Like

Posted 25 December 2013 - 02:51 PM

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.



#3 frob   Moderators   -  Reputation: 22737

Like
0Likes
Like

Posted 25 December 2013 - 04:09 PM

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 undefined.


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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#4 Paradigm Shifter   Crossbones+   -  Reputation: 5436

Like
0Likes
Like

Posted 25 December 2013 - 07:46 PM

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

#5 shadowstep00   Members   -  Reputation: 540

Like
0Likes
Like

Posted 26 December 2013 - 10:13 AM

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


Failure is not an option...


#6 nuclearfission   Members   -  Reputation: 119

Like
0Likes
Like

Posted 30 December 2013 - 01:40 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS