Solved

gets()  not working..

Posted on 2003-12-01
10
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

617 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