By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

gets and cin

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a
> "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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.