Hey guys, I am trying to self teach C++ and I am to the point of loops and the any inquiry. As you can see below this program checks to see if the numbers are greater than zero, and if there are any negative numbers, then the program terminates once the sentinel value is reached.
CODE
/* This program is a test driver for anyPositiveEOF.
Written by: Chubbs1900
Date: 12/05/07
*/
#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
*/
(As you can see, I am getting to know end of file statements as well.. But I am doing ok with that.)
Now what I want to do with this is to be able to incorporate the ability to return a negative number if there are any negative numbers, but if they are all positive numbers, then the program will return their average. Can someone point me in the right direction?