Thread: need help with program please

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    need help with program please

    This is a lab that i had last week but, i still can't figure it out. Problem: Allow the user to enter positive and negative integers and end the sequence with input of 0. Output the averages of positive, negative, and total integers. If no positive or negative integers are entered then display no positive or negative integers where entered.

    ex. Enter a sequence of integers, hit 0 key to stop: 4 4 4 0
    The positive average is: 4
    There were no negative integers entered
    The total average is: 2

    My problem is that I can't figure out how to display that there were no positive or negative integers entered. I'm pretty sure that it has something to do with an if statement. Any help would be greatly appreciated. Here's as far I have gotten with my code:
    Code:
    int main()
    {
    	
    	int n = 0; // user input
    	int pSum = 0, nSum = 0, pcount=0, ncount=0;
    
    	double pAverage = 0, nAverage = 0, totalAverage = 0; // positive and negative averages
    
    	cout << "Enter a sequence of integers, hit 0 key to stop: " ;
    	cin >> n; // user input
    while ( n != 0 )
    	{
    		if( n > 0 ) // if input is positive
    			pAverage = double( pSum += n) / ++pcount; // operation for average
    		if( n < 0 ) // if input is negative
    			nAverage = double( nSum += n) / ++ncount;
    		
    		cin >> n;
           
    }
    		totalAverage = double(pAverage + nAverage) / 2;
    
    		
    
    		if ( n == 0 ) // end sequence
    		{
    			
    
    			cout << "The positive average is: " << pAverage << endl;
    			cout << "The negative average is: " << nAverage << endl;
    			cout << "The total average is: " << totalAverage << endl;
    			
    		
    		}
    			
    		else 
    		{
    			cout << "Invalid integers entered" << endl;// invlaid entry
    		}
    
    
    		
    	return 0;
    	}
    Last edited by dgjenkin; 10-12-2006 at 11:29 PM. Reason: attachment file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM