Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,158 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,935 people online right now. Registration is fast and FREE... Join Now!




Loops: The 'any inquiry' 'all inquiry'

 
Reply to this topicStart new topic

Loops: The 'any inquiry' 'all inquiry', Combining a previouse tutorial with loops to better compriehend.

chubbs1900
5 Dec, 2007 - 09:06 PM
Post #1

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 9


My Contributions
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?


User is offlineProfile CardPM
+Quote Post

chubbs1900
RE: Loops: The 'any Inquiry' 'all Inquiry'
6 Dec, 2007 - 05:47 AM
Post #2

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 9


My Contributions
So does anyone see what I am trying to do??? Maybe I should try something else.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month