473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

gets and cin

Compiler: g++
code:

char data[30];
int e;
//..

gets(data);

// ..

cin >> e;

//..

gets (data);
problem: the second gets() is not working - not asking for data from
keyboard at least. When I delete line with cin it is working. What is
going on and how to improve it.

Thanks for help
Jul 22 '05 #1
8 2680
"Bartek" <zi****@students.cc.tut.fi> wrote:
Compiler: g++
code:
gets(data);
cin >> e;
gets (data);


You can say:

cin.sync_with_stdio()

at the top of your code to make stdin (which gets() uses) work with C++
streams ...

http://www.cppreference.com is a good library reference to look at.

David F
Jul 22 '05 #2
> "Bartek" <zi****@students.cc.tut.fi> wrote:
Compiler: g++
code:
gets(data);
cin >> e;
gets (data);


You can say:

cin.sync_with_stdio()

at the top of your code to make stdin (which gets() uses) work with C++
streams ...


By the way, did you know there is a cin.getline() function ?
Best to avoid mixing stdin and cin in general ...

David F
Jul 22 '05 #3
zi****@students.cc.tut.fi (Bartek) wrote:
char data[30];
int e;
gets(data);
cin >> e;
gets (data); problem: the second gets() is not working - not asking for data from
keyboard at least.


After reading the numeric input ('cin >> e') the stuff following is still
sticking in the input stream. In particular, the "\n" sought by 'gets()'
is still there. Thus, 'gets()' finds an empty line (the stuff following
the integer in the input) and happily finishes.

To avoid this problem, you should probably skip trailing garbage following
the numeric input. What exactly constitutes this garbage is somewhat
dependent on the use. The easiest would be to skip all whitespace following
the numeric input:

std::cin >> e >> std::ws;

This would, however, also skip empty lines, lines made up only of white
space, and leading whitespace. Often it is more desirable to only skip
the stuff up to and including the next newline:

std::cin >> e;
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');

BTW: 'gets()' is to be avoided in all cases! It is just present due to
legacy code (and it is legacy in all meanings of the word) which still
uses it. This function is extremely prone to buffer overruns and one of
the biggest offenders when it comes to whatever kind of attack. The
problem with this function is that you cannot pass the maximum size of
the buffer in. You should replace it by 'fgets()' if you want to really
use a C function or, IMO preferably, with 'std::getline()'.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #4

"David Fisher" <no****@nospam.nospam.nospam> wrote in message
news:BK******************@nasal.pacific.net.au...
| > "Bartek" <zi****@students.cc.tut.fi> wrote:
| >
| > > Compiler: g++
| > > code:
| > > gets(data);
| > > cin >> e;
| > > gets (data);
| >
| > You can say:
| >
| > cin.sync_with_stdio()
| >
| > at the top of your code to make stdin (which gets() uses) work with C++
| > streams ...
|
| By the way, did you know there is a cin.getline() function ?
| Best to avoid mixing stdin and cin in general ...

Since gets() is a dangerous function, even better would
be to not use it at all :-).

Cheers.
Chris Val
Jul 22 '05 #5
Dietmar Kuehl wrote:
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');


I thought that numeric_limits<int>::max() was the correct term (at least
according to C++98, 27.6.1.3). Has this changed in C++03?

Christoph
Jul 22 '05 #6
On Wed, 10 Dec 2003 15:15:42 +0100, Christoph Rabel
<od**@hal9000.vc-graz.ac.at> wrote:
Dietmar Kuehl wrote:
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');


I thought that numeric_limits<int>::max() was the correct term (at least
according to C++98, 27.6.1.3). Has this changed in C++03?


Yes. See
http://www.research.att.com/~ark/c++.../revisions.pdf

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #7
Ok, I've changed gets to cin.getline and I've put also ignore which
was still needed in my opinion.
Jul 22 '05 #8
zi****@students.cc.tut.fi (Bartek) wrote:
Ok, I've changed gets to cin.getline and I've put also ignore which
was still needed in my opinion.


Note that I didn't recommended 'std::cin.getline()' but rather
'std::getline()' which operates on 'std::string's:

std::string buffer;
std::getline(std::cin, buffer);

This approach allows [nearly] arbitrary length lines.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Marcus | last post by:
We all know that the "gets" function from the Standard C Library (which is part of the Standard C++ Library) is dangerous. It provides no bounds check, so it's easy to overwrite memory when using...
11
by: herrcho | last post by:
int get_lines(char *lines) { int n = 0; char buffer; puts("Enter one line at time; enter a blank when done"); while ((n < MAXLINES) && (gets(buffer) !=0 ) && (buffer != '\0')) { if ((lines...
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
52
by: Christopher Benson-Manica | last post by:
gets() is universally acknowledged to be broken and useless; however, it is still part of the standard library. Why? Is there enough conforming code out there using gets() to justify retaining...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
20
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 =...
280
by: jacob navia | last post by:
In the discussion group comp.std.c Mr Gwyn wrote: < quote > .... gets has been declared an obsolescent feature and deprecated, as a direct result of my submitting a DR about it (which...
104
by: jayapal | last post by:
Hi all, Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). why...? regards, jayapal.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.