Help with looping

I am working in a C++ intro course. My professor has asked me to create a program that will allow the user to input any number of rabbit weights in pounds into the program. The program will then output the average of all rabbit weights, the largest rabbit weight, and the total number of rabbits.

Here is what I have so far:

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int newBunny, bunnyCount;
	float bunnyWeight, largestBunny, averageBunny, totalBunnies;
	
	
	// begin loop for adding bunny weights
	cout<<"Please input a rabbit weight in pounds, or enter 0 to stop."<<endl;
	cout<<endl;
	cin>>bunnyWeight;
	while (bunnyWeight != 0){	
	cout<<"Please input a rabbit weight in pounds, or enter 0 to stop."<<endl;
	cout<<endl;
	cin>>bunnyWeight;
	}

	cout<<"The average weight of the rabbits is ">>averageBunny>>endl;
	cout<<endl;
	cout<<"The largest rabbit weighed "<<largestBunny<<" pounds."<<endl;
	cout<<endl;
	cout<<"The total number of rabbits weighed is "<<totalBunnies<<endl;
	cout<<endl;
	
	system ("pause");
	return 0;
}


In order to find the average weight, largest weight, and number of rabbits input, I know I need some piece of code that I am not familiar with to label each weight that is input. How do I do that?

(I can find the average weight, largest, etc. by myself. I just need to know what I am missing. Please don't help me with any more than that.)
Last edited on
The first thing that I see is that your variables averageBunny, totalBunnies and largestBunny are not initialized to anything. So if I were the user of the program, if I entered 0 for the first

1
2
3
cout<<"Please input a rabbit weight in pounds, or enter 0 to stop."<<endl;
cout<<endl;
cin>>bunnyWeight;


then your loop will never execute and I should see garbage values output when the program executes

1
2
3
4
5
6
cout<<"The average weight of the rabbits is ">>averageBunny>>endl;
cout<<endl;
cout<<"The largest rabbit weighed "<<largestBunny<<" pounds."<<endl;
cout<<endl;
cout<<"The total number of rabbits weighed is "<<totalBunnies<<endl;
cout<<endl;
Alright, I have changed the code around so that I have no garbage values and added a little more to the output section:

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int bunnyWeight=0;
	int largestBunny=0;
	int averageBunny=0;
	int totalBunnies=0;
	
	// begin loop for inputting bunny weights
	cout<<"Please input a rabbit weight in pounds, or enter 0 to stop."<<endl;
	cout<<endl;
	cin>>bunnyWeight;
	while (bunnyWeight != 0){	
	cout<<"Please input a rabbit weight in pounds, or enter 0 to stop."<<endl;
	cout<<endl;
	cin>>bunnyWeight;
		}
		
	averageBunny = (bunnyWeight / totalBunnies);
	cout<<"The average weight of the rabbits is "<<averageBunny<<endl;
	cout<<endl;
	largestBunny =
	cout<<"The largest rabbit weighed was "<<largestBunny<<" pounds."<<endl;
	cout<<endl;
	cout<<"The total number of rabbits weighed is "<<totalBunnies<<endl;
	cout<<endl;
	
	system ("pause");
	return 0;
}


My question now is, how to I get a value for totalBunnies (amount of rabbit weights input)?
Last edited on
In your while loop, provided it iterates, every time the user enters a weight != 0, add one to the total number of bunnies. However, if I still enter 0 for the first time I'm prompted for input, the while loop will be skipped and think about what happens when the line

 
averageBunny = (bunnyWeight / totalBunnies);


executes. Division by 0. If it were me, I would consider adding an "if-else" condition in the code, as in

1
2
3
4
if(bunnyWeight != 0)
    do this
else
    do that
Here is a short version of the program that you can reference to.

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int count = 0;
	float weight, totalweight = 0, largest = 0, average = 0;

	cin >> weight; // input the first weight
	if (weight != 0)
	{
		while (weight != 0)
		{
			count++;
			if (weight > largest)
				largest = weight;
			totalweight += weight;
			cin >> weight; // input weight
		}
		average = totalweight / count;
	}

	cout << "Total weight: " << totalweight << endl;
	cout << "Largest weight: " << largest << endl;
	cout << "Average weight: " << average;

	

	return 0;
}
Thank you, your example was perfect. I was able to plug what I was missing into my code and it works beautifully.
Topic archived. No new replies allowed.