Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

gets() not working..

hello!!

I want to input a string with spaces included.so i used gets() in place of scanf(). Its working correctly in one prog but in other prog, the program doesnt wait for the user input when i use gets(). scanf() is working fine. fflush(stdin) did not solve the purpose.What can be the problem?

Is there any alternative for gets() becoz during compilation also it gives a warning not to use gets()?

Paridhi
0
paridhi
Asked:
paridhi
2 Solutions
 
Karl Heinz KremerCommented:
Try fgets() instead. It takes three arguments:
char *fgets(char *s, int size, FILE *stream);

If your string is e.g. 80 characters long, and you are reading from stdin (which gets() does), use the following:

fgets(myString, 80, stdin);

0
 
shivsaCommented:
u can use getc also.
man getc
0
 
Karl Heinz KremerCommented:
getc() only returns one character at a time. This can certainly be used, if you run it in a loop. This is not a direct replacement for scanf. BTW: Your compiler complains about using gets() because you can overrun your buffer with the input. If e.g. you create a 80 character buffer, and you type 81 characters, getc() has no mechanism of knowing how much space is available, it will copy 81 one characters into your 80 character buffer. This of course means that the 81st character is written into a memory area that does not belong to the buffer. This problem is resolved by using fgets() which does the same as gets() if you read from stdin, but you do specify the size of your buffer, so the function can verify that your input string does actually fit.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
sunnycoderCommented:
use fgets() as khkremer showed or use scanf()
scanf ( "%[^\n]s", string );
will read until it encounters a newline
0
 
Karl Heinz KremerCommented:
Did it work?
0
 
paridhiAuthor Commented:
Finally i used getc() only in a for loop..But initially the problem was the same with getc() also. Actually both the getc() and gets() were taking the previous input's Enter as an input character  and so the program was not waiting for the user's input .So i read the previous enter in another variable before calling getc or gets. SO the problem finally got solved.Thanks for all ur help..

Paridhi
0
 
paridhiAuthor Commented:
Finally i used getc() only in a for loop..But initially the problem was the same with getc() also. Actually both the getc() and gets() were taking the previous input's Enter as an input character  and so the program was not waiting for the user's input .So i read the previous enter in another variable before calling getc or gets. SO the problem finally got solved.Thanks for all ur help..

Paridhi
0
 
Karl Heinz KremerCommented:
Don't use gets(). Your compiler is right, it's not a safe function. If you type more characters than you buffer can handle, your application wil crash - or even more dangerous, somebody would use this buffer overflow to potentially crack your computer (depending on what your program does).
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now