Thread: Loops: The 'any' inquiry

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    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
    */

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    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) {
    ..
    }

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You don't need the void in parenthesis either for that matter.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is this just a ported C program? That's the idea I get.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by iMalc View Post
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    AFAIK, () and (void) are the same thing in C++, but not in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 11:28 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM