Solved

gets()  not working..

Posted on 2003-12-01
10
189 Views
Last Modified: 2010-04-22
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
Comment
Question by:paridhi
10 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 9853653
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
 
LVL 24

Expert Comment

by:shivsa
ID: 9854578
u can use getc also.
man getc
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9854665
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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 125 total points
ID: 9855951
use fgets() as khkremer showed or use scanf()
scanf ( "%[^\n]s", string );
will read until it encounters a newline
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9890725
Did it work?
0
 

Author Comment

by:paridhi
ID: 9897371
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
 

Author Comment

by:paridhi
ID: 9897377
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9897636
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now