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

cin before fgets

P: n/a
given the following code snippet:

int num;

for (;;) {
cout << "enter number: ";
cin >> num;
//cin.clear();

while (fgets(line, MAXLINE, stdin) != NULL) {

after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin

how can i set the state of stdin so that after cin is called, fgets
blocks as expected?

Charles
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Charles Wilkins" <2boxers_at_comcast.net> wrote in message
news:et********************************@4ax.com...
given the following code snippet:

int num;

for (;;) {
cout << "enter number: ";
cin >> num;
//cin.clear();

while (fgets(line, MAXLINE, stdin) != NULL) {

after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin

how can i set the state of stdin so that after cin is called, fgets
blocks as expected?

Charles


What's expected? It doesn't block because there is still an unread newline
after the number. I/O in C and C++ is character based not line based. Use

cin.ignore(INT_MAX, '\n');

to ignore all pending input up to the next newline. #include <limits.h> for
the INT_MAX constant.

john


Jul 19 '05 #2

P: n/a
On Thu, 7 Aug 2003 09:12:02 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"Charles Wilkins" <2boxers_at_comcast.net> wrote in message
news:et********************************@4ax.com.. .
given the following code snippet:

int num;

for (;;) {
cout << "enter number: ";
cin >> num;
//cin.clear();

while (fgets(line, MAXLINE, stdin) != NULL) {

after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin

how can i set the state of stdin so that after cin is called, fgets
blocks as expected?

Charles


What's expected? It doesn't block because there is still an unread newline
after the number. I/O in C and C++ is character based not line based. Use

cin.ignore(INT_MAX, '\n');

to ignore all pending input up to the next newline. #include <limits.h> for
the INT_MAX constant.

john

this did what i was looking for. thank you.
Jul 19 '05 #3

P: n/a
Charles Wilkins wrote:

given the following code snippet:

int num;

for (;;) {
cout << "enter number: ";
cin >> num;
//cin.clear();

while (fgets(line, MAXLINE, stdin) != NULL) {

after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin


Aside from the comments made else-thread, a more fundumental question
is, why are you mixing stream and file I/O paradigms? Why not just use
one or the other?

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #4

P: n/a
On Thu, 07 Aug 2003 08:55:24 GMT, to********@hotmail.com (tom_usenet)
wrote:
On Thu, 07 Aug 2003 03:33:48 -0400, Charles Wilkins
<2boxers_at_comcast.net> wrote:
given the following code snippet:

int num;

for (;;) {
cout << "enter number: ";
cin >> num;
//cin.clear();

while (fgets(line, MAXLINE, stdin) != NULL) {

after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin

how can i set the state of stdin so that after cin is called, fgets
blocks as expected?


The problem is that, after your call to cin >> num, you've left a
newline character in the stream, which fgets reads in immediately and
returns. So all you need to do if empty that newline from the stream:

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

Also, why are you mixing C stdio and iostreams? It will work, but
what's wrong with getline? e.g.

std::string line;
while (std::getline(std::cin, line)) {

Note you still need the ignore code, since whether you are using stdio
or iostreams, the rest of the last line that you passed to the
terminal will still be there.

Tom

nothing is wrong with getline.
the fgets loop was part of an older model.
i simply was adding to this model as part of an exercise. for this
purpose I chose cin as a matter of preference.

beyond the scope of this experiment / exercise, i do not intentionally
seek to mix C stdio and iostreams

thanks,
Charles
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.