Loops: The 'any' inquiry

This is a discussion on Loops: The 'any' inquiry within the C++ Programming forums, part of the General Programming Boards category; Alright, in this situation I want to take a program that I wrote and modify it so it can take ...

  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,167
    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,292
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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,211
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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
    21,397
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:18 AM
  3. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 10:28 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21