The gets() call always gets ignored due to the carriage return or new line. What is the prosper way to use the gets() function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The gets() call always gets ignored due to the carriage return or new line. What is the prosper way to use the gets() function?

Please don't tell me any complicated alternative methods to get string input. I've just started learning C

7th Dec 2017, 11:54 AM
Arif Badar
Arif Badar - avatar
1 Answer
+ 4
You may try adding this before your gets() call: fflush(stdin); Basically, what this does is clear the standard input buffer. All your data from reading operations on console is saved to a buffer, and read from there. So if you had a read operation before gets(), a \n is left there and will skip your gets(). This statement clears the buffer so that it becomes empty. Thus, your gets now works.
8th Dec 2017, 10:26 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar