# Loops: The 'any' inquiry

• 12-09-2007
chubbs1900
Loops: The 'any' inquiry
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:

Code:

```/*        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 */```
• 12-09-2007
cyberfish
Quote:

Code:

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

you don't need that. There is a native "bool" type in C++.

as for how to know if theres any neg #,
Code:

`bool anyNegativeNumber = false;`
change it to true when you encounter one.
Then
Code:

```if (anyNegativeNumber) { .. }```
• 12-09-2007
iMalc
You don't need the void in parenthesis either for that matter.
• 12-10-2007
Elysia
Is this just a ported C program? That's the idea I get.
• 12-10-2007
MacGyver
Quote:

Originally Posted by iMalc
You don't need the void in parenthesis either for that matter.

Correct. C++ and C have different meanings for empty ()'s in a function definition.
• 12-10-2007
Elysia
AFAIK, () and (void) are the same thing in C++, but not in C.
• 12-10-2007
laserlight
Quote:

AFAIK, () and (void) are the same thing in C++, but not in C.
I believe they have the same meaning in C++ and C in a function definition, but not a function prototype.