MS VC++ cin.eof ignoring first ^Z. What up?

This is a discussion on MS VC++ cin.eof ignoring first ^Z. What up? within the C++ Programming forums, part of the General Programming Boards category; A buddy of mine is writing a little program in MS VC++ that takes input using cin. The problem he ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Question MS VC++ cin.eof ignoring first ^Z. What up?

    A buddy of mine is writing a little program in MS VC++ that takes input using cin. The problem he is having is that it will read in a file redirected in to it perfectly but when he uses the keyboard for input the program ignores the first eof character. He asked me to take a peek at it and it stumped me too. Now it's bugging me. So I decided to play around with cin a little. I compiled this very short program (see below) to see how it behaved when entering a ^Z from the keyboard. When I compile it with the MS VC++ compiler the binary has the same extact problem as my pal's program. I have to enter two ^Z's on lines by themselves in order the get the program to recognize the eof. Debugging shows that it ignores the first one. I fiddled with cin.ignore and cin.fail and cin.clear. Nothing made a difference.

    Here's the strange part. When I compile the exact same source file using MingW (a Win32 port of gcc) it works perfectly! It recognizes the first ^Z and exits exactly as it should.

    Does anybody have any idea why the MS comiled program would behave this way but the MingW binary would work perfectly when compiling the exact same source file?

    Thanks for the help, Don

    Here's the little sample:

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    int count = 0;
    string word;

    cin >> word;

    while (!cin.eof())
    { count++;
    cin >> word;
    }

    cout << count << " words.\n";
    return 0;
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Some of the stream libraries provided with MSVC have bugs in them. I'm not sure if this is one of them but you may want to take a look at http://www.dinkumware.com to see if you can find anything as they wrote the libraries and have provided fixes. Failing that, you could do something like this as a workaround -

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std; 
    
    int main() 
    { 
    
    	int count = 0;
    	string word; 
    
    
    	while (getline(cin,word,' ')) 
    		count++; 
    
    	cout << count << " words.\n"; 
    	return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Difference in MSVC 6 & MS VC .Net
    By passionate_guy in forum C Programming
    Replies: 1
    Last Post: 01-23-2006, 05:39 AM
  3. Ping
    By ZakkWylde969 in forum Tech Board
    Replies: 5
    Last Post: 09-23-2003, 12:28 PM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. Sun win over Microsoft
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 01-01-2003, 11:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21