cin before fgets

  • Thread starter Charles Wilkins
  • Start date
C

Charles Wilkins

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
 
J

John Harrison

Charles Wilkins said:
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
 
C

Charles Wilkins

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

David Rubin

Charles said:
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
 
C

Charles Wilkins

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top