Thread: EOF messing up my input stream?

  1. #1
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300

    EOF messing up my input stream?

    I am writing a program as an exercise from Accelerated C++. (3-5)

    I'm inputting names into a vector of strings, and so far the book has used/suggested
    Code:
    while (cin >> x)
        myVector.push_back(x);
    as the way to fill the vector. When the user enters end-of-file (Ctrl-Z for me) the loop is terminated.

    In my program, the user inputs additional information to be stored in a different vector. However, it never stops to get the input and crashes as it tries to do calculations with what should have been the additional input. I tested a few things and it seems that the additional variables are filled with crazy numbers. I thought it was somehow skipping the input and leaving in the variables the garbage that was assigned to the variables since I didn't initialize them myself. I tried initializing the variables to a number (they're doubles) but the same numbers keep popping up. Therefore, I assume that it really *is* drawing from the input buffer, just not anything the user has inputted. The only reason for this that I can think of is that the EOF used to terminate the first loop is closing off or messing up or something the input stream that I intend to use later on in the program.

    I realize that I could have the user input something like "done" in the first loop to terminate it that way instead of the EOF method, but I'm reading this book to overcome some bad habits, so I only use what the book has taught me so far, even if I know it is standard code and will work OK. Thankfully, I can use if statements, but I can only assume that the authors intended for me to use the EOF method since they haven't really discussed the input steam, buffers, the true nature of EOF, or any other way to fill a vector with an unknown number of variables - which means there should be (shouldn't there?) a way to do it like this.

    All of this babbling leads to this: Is the EOF used to terminate an input loop early in the program inhibiting me from inputting data later on? If not, why might it be filling my variables with strange numbers and crashing? I googled, I searched the boards and the FAQ (but maybe for the wrong things....)

    BTW, I'm using Dev C++ 4, on XP.
    There is a difference between tedious and difficult.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use cin.clear() to clear the stream of the fail/eof bits after the first loop ends.

    P.S. Wow, 500000th post.
    Last edited by Daved; 09-30-2005 at 02:18 PM.

  3. #3
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Quote Originally Posted by Daved
    P.S. Wow, 500000th post.
    huh?

    cin.clear() worked, thanks. But, the book hasn't gone over any input/output statements other than cin and cout. Now that I think again, I can restructure my program so that the EOF is the last the the user inputs.
    Instead of:
    Code:
    loop to get students' names (broken by EOF)
    loop to get students' grades (broken by EOF)
    I can do:
    Code:
    loop to get everything (broken by EOF)
    {
        get a student's name
        get that student's grades
    }
    Thanks again for your help.

    Decrypt
    Last edited by Decrypt; 09-30-2005 at 02:41 PM. Reason: oops
    There is a difference between tedious and difficult.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's a good choice. The answer to your original question was yes, the eof was inhibiting further input from the stream, which is why the clear() allowed it to continue.

    The 500000th post reference was to the post Id in the address (http://cboard.cprogramming.com/showt...000#post500000). Apparently my response was the 500000th post on this forum, or since the latest database, or something like that. Not as cool as 1 millionth, but Id on't know if I'll be around to see that.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    OK, thanks again for your help. I was hoping that this wasn't the 500000th time someone asked that question...
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 12:39 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. h/w help
    By helpme in forum C Programming
    Replies: 20
    Last Post: 10-21-2003, 09:36 AM