end of file... weird action

This is a discussion on end of file... weird action within the C++ Programming forums, part of the General Programming Boards category; Im compiling this with visual c++ .net and when i try and end the while loop with the end of ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    end of file... weird action

    Im compiling this with visual c++ .net and when i try and end the while loop with the end of file character ( control + z ) it drops me completely out of the console window.

    Code:
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    // debug
    #define DEBUG
    
    using std::cin;		using std::setprecision;
    using std::cout;	using std::string;
    using std::endl;	using std::streamsize;
    
    int main()
    {
    	// ask for / read students name
    	cout << "Please enter your first name: ";
    	string name;
    	cin >> name;
    
    	cout << "Hello, " << name << "!" << endl;
    
    	// ask for / read mid term and final grades
    	cout << "Please enter your mid-term and final grades: ";
    	double mid, final;
    	cin >> mid >> final;
    
    	// +++++++++++++ DEBUG
    	#ifdef DEBUG
    	cout << "before while loop" << endl;
    	cout << "enter to confirm" << endl;
    	cin.ignore();
    	cin.get();
    	#endif	
    	// ------------- eof DEBUG
    
    	// ask for homework grades
    	cout << "Enter all your homework grades, "
    			"followed by end-of-file: ";
    
    	// the number and sum of grades read so far
    	int count = 0;
    	double sum = 0.0;
    
    	// variable to temp the homework grades
    	double x;
    	
    	// gather data
    	while (cin >> x)
    	{
    		++count;
    		sum += x;
    	}
    
    	// +++++++++++++ DEBUG
    	#ifdef DEBUG
    	cout << "after while loop" << endl;
    	cout << "enter to confirm" << endl;
    	cin.ignore();
    	cin.get();	
    	#endif	
    	// ------------- eof DEBUG
    	
    	// write result 
    	streamsize prec = cout.precision();
    	cout << "Your final grade is " << setprecision(3)
    		<< (0.2 * mid) + (0.4 * final) + ((0.4 * sum) / count) 
    		<< setprecision(prec) << endl;
    
    	// pause
    	cin.ignore();
    	cin.get();
    
    	return 0;
    }
    I thought this was something really stupid that i was missing but i loaded this cpp file up on my laptop and compiled it with microsoft visual c++ 6 ( the version before .net ) and it worked.... when i entered the end of file character ( control + z ) it carried on with the program and printed out my results.

    Any ideas how to make this work on my visual studio .net version???????

    also this is all on windows xp

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,649
    You probably need a cin.clear() after the ctrl-z.

    Even better would be to have them enter -1 instead of end-of-file.

    gg

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2
    you are a genius, that cin.clear() seemed to fix her right up, thx!

    What was actually happening that made it necessary to use the cin.clear()? so that i might avoid that same problem in programs to come.

    thanks for solving my problem

    later
    sifurat

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > What was actually happening that made it necessary to use the cin.clear()?
    Well you typed ctrl-z, which is end of file
    EOF is a sticky state, so any cin which follows it will immediately return EOF as well, up until you clear that condition.

    Looks like VC6 was the buggy version, even though it did what you wanted
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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