cin before fgets

Discussion in 'C++' started by Charles Wilkins, Aug 7, 2003.

  1. 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
    Charles Wilkins, Aug 7, 2003
    #1
    1. Advertising

  2. "Charles Wilkins" <2boxers_at_comcast.net> wrote in message
    news:...
    > 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
    John Harrison, Aug 7, 2003
    #2
    1. Advertising

  3. On Thu, 7 Aug 2003 09:12:02 +0100, "John Harrison"
    <> wrote:

    >
    >"Charles Wilkins" <2boxers_at_comcast.net> wrote in message
    >news:...
    >> 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.
    Charles Wilkins, Aug 7, 2003
    #3
  4. Charles Wilkins

    David Rubin Guest

    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
    David Rubin, Aug 7, 2003
    #4
  5. On Thu, 07 Aug 2003 08:55:24 GMT, (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
    Charles Wilkins, Aug 7, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,086
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,131
    Kevin Saff
    Jan 6, 2004
  3. Mike Mimic

    fgets and newline

    Mike Mimic, May 15, 2004, in forum: C++
    Replies:
    4
    Views:
    7,980
    John Harrison
    May 15, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,656
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,640
    Fernando
    Nov 16, 2011
Loading...

Share This Page