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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

// This program uses a private Boolean function to determine if 
// a new value sent to it is the largest value received so far

#include <iostream>
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<double>(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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

// This program uses a private Boolean function to determine if 
// a new value sent to it is the largest value received so far

#include <iostream>
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<double>(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.

1
2
3
4
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.