### Purpose of Bool Function

In the following sample program, I'm not entirely sure why

 `` `` `` bool addNumber(int);``

is a Boolean function. The code would work just fine if this function were defined as void.

Here is the original:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136`` `````` // This program uses a private Boolean function to determine if // a new value sent to it is the largest value received so far #include using namespace std; class SimpleStat { private: int largest; // The largest number received so far int sum; // The sum of the numbers received int count; // How many numbers have been received bool isNewLargest(int); public: SimpleStat(); // Default constructor bool addNumber(int); double getAverage(); int getLargest() { return largest; } int getCount() { return count; } }; // SimpleStat Class Implementation Code /* =================== SimpleStat Default Constructor Initialize the three variables to zero =================== */ SimpleStat::SimpleStat() { largest = sum = count = 0; } /* =================== SimpleStat::addNumber =================== */ bool SimpleStat::addNumber(int num) // function for adding the numbers { bool goodNum = true; if (num >= 0) // If num is valid { sum += num; // Add it to the sum count++; // Count it if(isNewLargest(num)) // Find out if it is largest = num; // the new largest } else // num is invalid goodNum = false; return goodNum; } /* =================== SimpleStat::isNewLargest =================== */ bool SimpleStat::isNewLargest(int num) { if (num > largest) return true; else return false; } /* =================== SimpleStat::getAverage =================== */ double SimpleStat::getAverage() { if (count > 0) return static_cast(sum) / count; else return 0; } // Client Program /* =================== Main =================== */ int main() { int num; SimpleStat statHelper; cout << "Please enter the set of non-negative integer \n"; cout << "values you want to average. Separate them with \n"; cout << "spaces and enter -1 after the last value. \n\n"; cin >> num; while (num >= 0) { statHelper.addNumber(num); cin >> num; } cout << "\nYou entered " << statHelper.getCount() << " values. \n"; cout << "The largest value was " << statHelper.getLargest() << endl; cout << "The average value was " << statHelper.getAverage() << endl; return 0; }``````

Here is the modified version defining addNumber as void:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137`` `````` // This program uses a private Boolean function to determine if // a new value sent to it is the largest value received so far #include using namespace std; class SimpleStat { private: int largest; // The largest number received so far int sum; // The sum of the numbers received int count; // How many numbers have been received bool isNewLargest(int); public: SimpleStat(); // Default constructor void addNumber(int); double getAverage(); int getLargest() { return largest; } int getCount() { return count; } }; // SimpleStat Class Implementation Code /* =================== SimpleStat Default Constructor Initialize the three variables to zero =================== */ SimpleStat::SimpleStat() { largest = sum = count = 0; } /* =================== SimpleStat::addNumber =================== */ void SimpleStat::addNumber(int num) // function for adding the numbers { // bool goodNum = true; if (num >= 0) // If num is valid { sum += num; // Add it to the sum count++; // Count it if(isNewLargest(num)) // Find out if it is largest = num; // the new largest } // else // num is invalid // goodNum = false; // return goodNum; } /* =================== SimpleStat::isNewLargest =================== */ bool SimpleStat::isNewLargest(int num) { if (num > largest) return true; else return false; } /* =================== SimpleStat::getAverage =================== */ double SimpleStat::getAverage() { if (count > 0) return static_cast(sum) / count; else return 0; } // Client Program /* =================== Main =================== */ int main() { int num; SimpleStat statHelper; cout << "Please enter the set of non-negative integer \n"; cout << "values you want to average. Separate them with \n"; cout << "spaces and enter -1 after the last value. \n\n"; cin >> num; while (num >= 0) { statHelper.addNumber(num); cin >> num; } cout << "\nYou entered " << statHelper.getCount() << " values. \n"; cout << "The largest value was " << statHelper.getLargest() << endl; cout << "The average value was " << statHelper.getAverage() << endl; return 0; } ``````

My only guess is that by defining addNumber as bool one can include an else statement. But this also produces a few lines of (seemingly) superfluous code -- unless this is considered cleaner programming.

Not sure.
SimpleStat::addNumber() only accepts positive numbers, so the variable sum keeps increasing and so isNewLargest(num) is also true unless a non-positive number is entered in which case the function returns – so is this one way of accepting positive numbers only from user and calculating the sum, average and the max sum?
Any yes, it would also work with addNumber() returning void and you could also consider spewing out the sum, count and average from within addNumber() itself
So then why would anyone use bool here?
 So then why would anyone use bool here?

that'd be one for your prof I suppose - an overdose of bool?
Maybe he's a bool.
Bool would be used so you can tell if the function succeeded or not. That way you can use an if statement to do something if the function failed.

 ``1234`` ``````if (!statHelper.addNumber(num)) // if failed { // do something; like request for input again }``````
 Bool would be used so you can tell if the function succeeded or not. That way you can use an if statement to do something if the function failed.

Thanks. I suspected this was being used to set something up that was implied.
 Bool would be used so you can tell if the function succeeded or not

all functions could be bool in this sense, rather void might be enough with this bit as suggested earlier that can tell whether the function ran successfully or not:
 with addNumber() returning void and you could also consider spewing out the sum, count and average from within addNumber() itself
Using a function that returns bool (or int) would make the code cleaner. (e.g. non-messy main function). On line 125 that condition wouldn't be needed. If the function is doesn't return anything then at least check the condition in another function.
Topic archived. No new replies allowed.