Average returning value of 0.

#include <iostream>
#include <iomanip>

using namespace std;

void welcome();
void getBabyWeight(double &, double &, double &, double &, double &);
void findLowest(double &, double &, double &, double &, double &);
void findHighest(double &, double &, double &, double &, double &);
void weightAverage(double &, double &, double &, double &, double &);

int main()
{

double baby1, baby2, baby3, baby4, baby5, average;
cout << fixed << showpoint << setprecision(2);
welcome();
getBabyWeight(baby1, baby2, baby3, baby4, baby5);
findLowest(baby1, baby2, baby3, baby4, baby5);
findHighest(baby1, baby2, baby3, baby4, baby5);
weightAverage(baby1, baby2, baby3, baby4, baby5);
cout << endl << "Today's average weight is " << average << endl;




}

void welcome()
{
cout << "***************************************";
cout << endl << "BABY WEIGHT PROGRAM" << endl;
cout << "Please enter the Five Baby Weights" << endl;
cout << "Please enter a real number Weights (Must be >0)." << endl;
cout << endl << "Program Developed by: Rohan S. Jakhete" << endl;
cout << "***************************************";
}

void getBabyWeight(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
cout << endl << "Please enter the values now";
cin >> baby1;
while (baby1 <= 0)
{
cout << "Invalid weight...weight must be greater than 0";
cin >> baby1;

}
cin >> baby2;
while (baby2 <= 0)
{
cout << "Invalid weight...weight must be greater than 0";
cin >> baby2;

}
cin >> baby3;
while (baby3 <= 0)
{
cout << "Invalid weight...weight must be greater than 0";
cin >> baby3;

}
cin >> baby4;
while (baby4 <= 0)
{
cout << "Invalid weight...weight must be greater than 0";
cin >> baby4;

}
cin >> baby5;
while (baby5 <= 0)
{
cout << "Invalid weight...weight must be greater than 0";
cin >> baby5;

}

}


void findLowest(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
if ((baby1 < baby2) && (baby1 < baby3) && (baby1 < baby4) && (baby1 < baby5)){
cout << endl << "Baby 1 has the lowest weight at " << baby1;
}

else if ((baby2 < baby1) && (baby2 < baby3) && (baby2 < baby4) && (baby2 < baby5)){
cout << endl << "Baby 2 has the lowest weight at " << baby2;
}

else if ((baby3 < baby1) && (baby3 < baby2) && (baby3 < baby4) && (baby3 < baby5)){
cout << endl << "Baby 3 has the lowest weight at " << baby3;
}

else if ((baby4 < baby1) && (baby4 < baby2) && (baby4 < baby3) && (baby4 < baby5)){
cout << endl << "Baby 4 has the lowest weight at " << baby4;
}

else if ((baby5 < baby1) && (baby5 < baby2) && (baby5 < baby3) && (baby5 < baby4)){
cout << endl << "Baby 5 has the lowest weight at " << baby5;
}

else
{
cout << endl << "There is a tie!! No lowest baby weight today";
}
}

void findHighest(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
if ((baby1 > baby2) && (baby1 > baby3) && (baby1 > baby4) && (baby1 > baby5)){
cout << endl << "Baby 1 has the highest weight at " << baby1;
}

else if ((baby2 > baby1) && (baby2 > baby3) && (baby2 > baby4) && (baby2 > baby5)){
cout << endl << "Baby 2 has the highest weight at " << baby2;
}

else if ((baby3 > baby1) && (baby3 > baby2) && (baby3 > baby4) && (baby3 > baby5)){
cout << endl << "Baby 3 has the highest weight at " << baby3;
}

else if ((baby4 > baby1) && (baby4 > baby2) && (baby4 > baby3) && (baby4 > baby5)){
cout << endl << "Baby 4 has the highest weight at " << baby4;
}

else if ((baby5 > baby1) && (baby5 > baby2) && (baby5 > baby3) && (baby5 > baby4)){
cout << endl << "Baby 5 has the highest weight at " << baby5;
}

else
{
cout << endl << "There is a tie!! No highest baby weight today";
}
}

void weightAverage(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
double average;
average == (baby1 + baby2 + baby3 + baby4 + baby5);

}

average == (baby1 + baby2 + baby3 + baby4 + baby5);

should be average = (baby1 + baby2 + baby3 + baby4 + baby5) / 5;

== means you are comparing, whereas =, you are assigning.



Oh, thanks so much! I didn't catch that.
Wait, actually, still getting zero
Not a problem. Good luck!
Wait, still getting zero....
Line 140: You're using the comparison operator (==), not the assignment operator (=).
Also, if you want to compute the average, you should divide by 5.

Line 141: average goes out of scope at the end of the function.

Line 15: average is an uninitialized variable.

Line 22: You're printing an uninitialized variable.

Have you learned arrays yet? This would be a whole lot simpler using arrays.

Lines 42-76: Do you see a lot of repeated code here? That's a good indication you need a function.

So I'm in a class and we are not allowed to use arrays for this specific homework. Also, I used functions, it just not visible on this website. What do you mean by average goes out of scope? I fixed the other issues.
Here is a way to do it. By changing the function type of weightAverage from void to double and returning the average to main.

Something like...

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <iomanip>

using namespace std;

void welcome();
void getBabyWeight(double &, double &, double &, double &, double &);
void findLowest(double &, double &, double &, double &, double &);
void findHighest(double &, double &, double &, double &, double &);
//void weightAverage(double &, double &, double &, double &, double &);
double weightAverage(double &, double &, double &, double &, double &);

int main()
{

	double baby1, baby2, baby3, baby4, baby5, average;
	cout << fixed << showpoint << setprecision(2);
	welcome();
	getBabyWeight(baby1, baby2, baby3, baby4, baby5);
	findLowest(baby1, baby2, baby3, baby4, baby5);
	findHighest(baby1, baby2, baby3, baby4, baby5);
	//weightAverage(baby1, baby2, baby3, baby4, baby5);
	average = weightAverage(baby1, baby2, baby3, baby4, baby5);
	cout << endl << "Today's average weight is " << average << endl;




}

void welcome()
{
	cout << "***************************************";
	cout << endl << "BABY WEIGHT PROGRAM" << endl;
	cout << "Please enter the Five Baby Weights" << endl;
	cout << "Please enter a real number Weights (Must be >0)." << endl;
	cout << endl << "Program Developed by: Rohan S. Jakhete" << endl;
	cout << "***************************************";
}

void getBabyWeight(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
	cout << endl << "Please enter the values now";
	cin >> baby1;
	while (baby1 <= 0)
	{
		cout << "Invalid weight...weight must be greater than 0";
		cin >> baby1;

	}
	cin >> baby2;
	while (baby2 <= 0)
	{
		cout << "Invalid weight...weight must be greater than 0";
		cin >> baby2;

	}
	cin >> baby3;
	while (baby3 <= 0)
	{
		cout << "Invalid weight...weight must be greater than 0";
		cin >> baby3;

	}
	cin >> baby4;
	while (baby4 <= 0)
	{
		cout << "Invalid weight...weight must be greater than 0";
		cin >> baby4;

	}
	cin >> baby5;
	while (baby5 <= 0)
	{
		cout << "Invalid weight...weight must be greater than 0";
		cin >> baby5;

	}

}


void findLowest(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
	if ((baby1 < baby2) && (baby1 < baby3) && (baby1 < baby4) && (baby1 < baby5)) {
		cout << endl << "Baby 1 has the lowest weight at " << baby1;
	}

	else if ((baby2 < baby1) && (baby2 < baby3) && (baby2 < baby4) && (baby2 < baby5)) {
		cout << endl << "Baby 2 has the lowest weight at " << baby2;
	}

	else if ((baby3 < baby1) && (baby3 < baby2) && (baby3 < baby4) && (baby3 < baby5)) {
		cout << endl << "Baby 3 has the lowest weight at " << baby3;
	}

	else if ((baby4 < baby1) && (baby4 < baby2) && (baby4 < baby3) && (baby4 < baby5)) {
		cout << endl << "Baby 4 has the lowest weight at " << baby4;
	}

	else if ((baby5 < baby1) && (baby5 < baby2) && (baby5 < baby3) && (baby5 < baby4)) {
		cout << endl << "Baby 5 has the lowest weight at " << baby5;
	}

	else
	{
		cout << endl << "There is a tie!! No lowest baby weight today";
	}
}

void findHighest(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
	if ((baby1 > baby2) && (baby1 > baby3) && (baby1 > baby4) && (baby1 > baby5)) {
		cout << endl << "Baby 1 has the highest weight at " << baby1;
	}

	else if ((baby2 > baby1) && (baby2 > baby3) && (baby2 > baby4) && (baby2 > baby5)) {
		cout << endl << "Baby 2 has the highest weight at " << baby2;
	}

	else if ((baby3 > baby1) && (baby3 > baby2) && (baby3 > baby4) && (baby3 > baby5)) {
		cout << endl << "Baby 3 has the highest weight at " << baby3;
	}

	else if ((baby4 > baby1) && (baby4 > baby2) && (baby4 > baby3) && (baby4 > baby5)) {
		cout << endl << "Baby 4 has the highest weight at " << baby4;
	}

	else if ((baby5 > baby1) && (baby5 > baby2) && (baby5 > baby3) && (baby5 > baby4)) {
		cout << endl << "Baby 5 has the highest weight at " << baby5;
	}

	else
	{
		cout << endl << "There is a tie!! No highest baby weight today";
	}
}

//void weightAverage(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
//{
//	double average;
//	average = (baby1 + baby2 + baby3 + baby4 + baby5) / 5;
//
//}

double weightAverage(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
{
	double average;
	average = (baby1 + baby2 + baby3 + baby4 + baby5) / 5;

	return average;
}
Thank you up hand. But, how do you fix the average that goes out of scope
Wow, thank you! I hope to get as good as C++ as you!!
But, how do you fix the average that goes out of scope


On line 141, you declare the variable average; however, the value on line 142 does not go anywhere. You did not pass the average value by reference or did not return the value.

I changed the function type from
void weightAverage(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)
to
double weightAverage(double &baby1, double &baby2, double &baby3, double &baby4, double &baby5)

so you can return the average value back to the calling function in line 22, which then, it prints out the average on line 24.
Thank you!
Topic archived. No new replies allowed.