Alright, in this situation I want to take a program that I wrote and modify it so it can take an integers and if any of the numbers are negative, it returns a negative number, but if all the numbers are positive, it returns their average. This is the program I would like to modify:

/*	This program is a test driver for anyPositiveEOF.
	   Written by: Chubbs1900
	   Date: 12-01-2007
*/
#include <iostream>
using namespace std;

#ifndef TRUE
	#define  TRUE  1
	#define  FALSE 0
#endif

//	Prototype Statements
	bool anyPositiveEOF (void);

int main (void) 
{
//	Statements
	cout << "Enter numbers <EOF> to stop:\n";
	cout << "anyPositiveEOF is: " << anyPositiveEOF () << endl;
	return 0;
}	// main

/*	================= anyPositiveEOF ==================
	Read number series & determine if any are positive.
	   Pre   Nothing
	   Post  Returns true  if any numbers >  0
	         Returns false if all numbers <= 0
*/
bool anyPositiveEOF (void)
{
	bool anyPositive = false;
	int  numIn;
	while ( !anyPositive && (cin >> numIn) )
	    anyPositive = (numIn > 0);
	return anyPositive;
} // anyPositiveEOF

/* Results:
Enter numbers <EOF> to stop:
-1
-2
3
anyPositiveEOF is: 1

Enter numbers <EOF> to stop:
-1
-2
-3
^danyPositiveEOF is: 0
*/

Have you made an attempt to modify the program to do so? Please post that.

Val

Well i have removed

#ifndef TRUE
	#define  TRUE  1
	#define  FALSE 0
#endif

cause i realized i didnt need it.. then as for how to know if theres any neg #

bool anyNegativeNumber = false;

then change it to true when i encounter one, then

if (anyNegativeNumber) {
..
}

My major issue is getting the program to return the negative number if its negative, but then return the average if they are all positive... my EOF part isnt working right to tell the program to stop cin-ing positive int's and average them

Your function needs to be changed to have an int return type. As you go through the loop reading in numbers, test them for negative value. If so, set a flag to note that a negative was encountered. Keep a running sum of the numbers as you read them in and a count of how many have been read. When the user has ended the input, test the flag. If no negatives encountered, calculate and return the average, otherwise return -1.

Your function needs to be changed to have an int return type. As you go through the loop reading in numbers, test them for negative value. If so, set a flag to note that a negative was encountered. Keep a running sum of the numbers as you read them in and a count of how many have been read. When the user has ended the input, test the flag. If no negatives encountered, calculate and return the average, otherwise return -1.

int anyPositiveEOF (void)
{
	int anyPositive = 0;
	int numIn;
	int counter;
	for ( counter = 0; cin >> numIn; counter++ )
		if ( numIn > 0 )
			anyPositive += numIn;
		else
			return -1;
	return anyPositive / counter;
}

This what you mean?

Yes, that works. What about an input of 0 (zero)?

Just for discussion, I'm not fond of for loops that exit for reasons other than a counted method. You are using a test condition not related to the counter, and you have a return inside the loop. Consider:

int anyPositiveEOF (void)
{
	int sum = 0;
	int numIn;
	int counter = 0;
        bool negativeFound = false;
	while (  cin >> numIn )
       {
	   
            if ( numIn >= 0 )
            {
                 counter++;
		sum += numIn;
             }
	    else
		negativeFound = true;
        }

         if( negativeFound)
             return -1;
         else
	    return sum / counter;
}

Do you in fact want the integer average, or a floating point value that will be more accurate?

Val

int anyPositiveEOF (void)
{
	int anyPositive = 0;
	int numIn;
	int counter;
	for ( counter = 0; cin >> numIn; counter++ )
		if ( numIn > 0 )
			anyPositive += numIn;
		else
			return -1;
	return anyPositive / counter;
}

This what you mean?

How do you exit the loop when you don't want negative numbers?

This article has been dead for over six months. Start a new discussion instead.