Discussion in 'Programmer's Corner' started by afaik, Jan 14, 2009.

  1. afaik

    Thread Starter Member

    Nov 2, 2008
    I'm using dev-c++ and when I input characters into the code below, for example 'asdf', it loops fine. If I enter 'asdf(ctrl-z)' for end of file, program will output the asdf, but wont display cout << "eof reached" or exit the loop. If I enter ctrl-z by itself, program will output cout << "eof reached" and exit the loop. Can anyone point out what I'm doing wrong?

    -I probably could do this better with a switch, that would be tomorrows exercise

    Code ( (Unknown Language)):
    1. #include <iostream>
    3. using namespace std;
    5. int main()
    6. {
    8. char temp;
    9. char nwln = '\n';
    11. cout << "EOF: " << EOF << endl;
    12. cout << "Input something: ";
    14. //while(nwln != temp)
    15. do
    16. {
    17.   temp = cin.get();
    18. //  cin >> temp;
    20.   if(temp != '\n' && temp != EOF)  //cin.eof() )
    21.     cout << "You inputted:" << temp << "." << endl;
    23.   if(temp == '\n' )
    24.     cout << "New Line entered" << endl;
    26.   if(cin.eof() ) // if(temp == -1)  // if(temp == EOF)
    27.     cout << "END OF FILE REACHED";  
    29. }while(!cin.eof() );  
    31. cout << "\n\n" ;
    32. system("PAUSE");
    33. }  
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    While I don't know C++, I'd suggest to begin by simplifying the loop:
    Code ( (Unknown Language)):
    2. do{
    3. }while(!cin.eof() );
    4. cout << "Got EOF\n" ;
    ... and make sure it works. Then add the tests singly -- one and only one test in the loop at a time. I'd even split the multiple if statement. This way you can find out which statement is giving you trouble. If they all pass on their own, then there may be an ordering problem.

    My guess is that "temp = cin.get();" is eating the EOF character, however, is IS a guess.


  3. Mark44

    Well-Known Member

    Nov 26, 2007
    Rich's advice is good. I would also add that the two calls to cin.eof() in each loop iteration might be causing problems.

    You said that you are using a dev-C++ compiler. I've never heard of it, but if you have any documentation on the behavior of the eof() function, it might be helpful in understanding what's going on here. Each compiler vendor implements the run-time classes in its own way, so without having that compiler I can't say what the behavior is. In any case, my guess is that after you have called eof() the first time, you shouldn't be calling eof() a second time if there has been no change in the stream you're reading from.

    If your compiler comes with a debugger, and you know how to use it, that would be extremely useful. You could use it to single-step through the code to see what happens to the EOF character (CTRL-Z) in the two cases you tested.
  4. afaik

    Thread Starter Member

    Nov 2, 2008
    Something definitely funny is happening at "temp = cin.get();" After playing around a bit with the debugger (that really is useful), I need to do reading and see how cin.get() will handle an EOF value. Because I'm saving to a char and an EOF, according to my somewhat outdated book, says it "returns non-zero (true) if eof has been encountered in designated stream". I'll try again later tonight for a bit, hopefully I can make progress with it and continue learning instead of being stuck on the same spot. I'll post my solution if I find it.
  5. afaik

    Thread Starter Member

    Nov 2, 2008
    Unbelievable...After playing around with this for two weeks and learning NOTHING, I've found some info that might help anyone else having problems with EOF on a windows machine.

    Bjarne Stroustrup:
    The program ends reading input when it sees "end of file". If you run the program from the keybord on a Unix machine "end of file" is Ctrl-D. If you are on a Windows machine that because of a bug doesn't recognize an end-of-file character...
    (Scroll to last bullet of that part)

    Google Groups:
    I have a problem with a C++ program under Windows but which works fine
    under Linux. The program simply reads in integers from a file (source
    code below)...
    In Linux it reads all the numbers in the file no matter how EOF is
    positioned: right after the last number or on the next line in
    in_file. In Windows it won't read the last number unless there's an
    empty line at the end of the file.
    1 2 3 4 5 6 7EOF -> this works under Linux but not under Windows
    1 2 3 4 5 6 7<CR>
    EOF -> this works under both OS's "