Thread: ctrl-d problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    ctrl-d problem

    Hey what's goin guys?

    OK I got a real quick question for you all that has been bothering me. How can I get the EOF (or ctrl-d) to work on my C++ program. I know in C you can use getchar, but I am using integers as input on this program.

    Any advice/hints on how to fix this?

    Code:
     #include<iostream.h>
    #include<stdio.h>
    
    void print( int min2, int max2, int n=5);	//Declaration
    
    int main()
    {
    //	system("clear");
    	
    	int q=0, j=0, y=0, min=0, middle=0, max=0;
    
    	cout<< "\n\t-This program prints out the default and middle amount of numbers-";
    	
    	cout<< "\n\n\tEnter (<ctrl-d> <return>) to quit the program.\n";
    	
    	cout<< "\n\n\tEnter three numbers with spaces: ";
    	cin>> q >> j >> y;
    	fflush(stdin);
    	
    	while( q != !EOF )
    	{
    
    	if( q > j)			//Checks for max value
    	{
    		if(q > y)
    			max = q;
    
    		else
    			max = y;
    
    	}
    
    	else
    	{
    		if(j > y)
    			max = j;
    		else
    			max = y;
    	}
    
    	
    	if( q < j)				//Checks for min value
    	{
    		if(q < y)
    			min = q;
    
    		else
    			min = y;
    
    	}
    
    	else
    	{
    		if(j < y)
    			min = j;
    		else
    			min = y;
    	}
    
    	
    	if ( max == q )				//Uses max & min to find middle 
    	{
    		if ( min == y )
    			middle = j;
    		else
    			middle = y;
    	}
    
    	if ( max == y )
    	{
    		if( min == q)
    			middle = j;
    
    		else
    			middle = q;
    
    	}
    
    	else if( max == j)
    	{
    		if ( min == q )
    			middle = y;
    		else
    			middle = q;
    	}
    
    	cout<< "\n\tUsing default line-set (5)";
    	cout<< "\n\tPrint 5 per line...\n";
    	print(min, max );			//Use print function with default line-set
    
    //	system("sleep 3");
    	cout<< "\n\n\tNext line-set...";
    	
    	cout<< "\n\tUsing line-set " << middle;
    	cout<< "\n\tPrint " << middle << " per line...\n";
    	print(min, max, middle);	//Use print function with middle as line-set
    
    	cout<< "\n\n\tEnter three numbers with spaces: ";
    	cin>> q >> j >> y;
    	fflush(stdin);
    	
    	}
    
    	printf("\n\n\tPress enter to exit...\n");
    	getchar();
    
    return 0;
    }
    
    void
    print( int min2, int max2, int n)			//Definition
    {
    	int count=0;
    
    	cout<< "\n\t";
    	
    	for( int i = min2; i <= max2; i++)
    	{
    			cout<< i << " ";
    			count++;
    
    			if (count == n)		//Checks for new line
    			{	
    				cout<< "\n\t";
    				count=0;
    			}
        }
    
    	cout<< "\n";
    }
    CJ
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    while( cin>> q >> j >> y )
    If cin fails then either end of file was reached or an input error occurred. You can test for that after the loop body and respond accordingly. Otherwise the loop will proceed normally.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Thank you very much Prelude.

    I was trying:

    Code:
     while( cin.eof )
    also after I posted this. However it gave off the same results. I looked in my C++ book and says it is suppose to reconize EOF, but for some reason didn't work either.

    Well thanks again!

    CJ
    "If it is too loud, your too old!" -unknown
    "You mean Honda Civics are not the almightiest of cars?" -My friend


    One of the funniest things I have ever heard:
    Guy at car show: "Damn that thing must do wicked burn outs."
    Me: "That is a STI! It is All Wheel Drive!"
    Guy: "Yeah...so?"

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while( cin.eof )
    The correct (syntactically at least) way would be:
    Code:
    while ( !cin.eof() )
    But this member function was not designed to be used as a loop condition and you will end up processing the last piece of input twice. Whenever possible, make it so that the call for input is the loop condition so that a success/failure return value can be used to determine whether to iterate again or to terminate the loop.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM