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.
#include <iostream>
#include <cstdlib>
usingnamespace 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.)
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;
#include <iostream>
#include <cstdlib>
usingnamespace 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)?
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