Oct 13, 2012 at 7:27am UTC
I'm trying to compute the average of the data that the user puts in, using a while loop, but I cannot seem to get it right. Can someone please help me fix the error? Thank you.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout.setf(ios::left);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout.width(10);
double weight;
cout <<"Enter your weights: ";
cin >> weight;
cout << endl;
int category;
int count = 0;
double sum = 0;
double shipping;
double total = 0;
double min = 1000, max = 0.00000001;
//Processing Section
cout<<"Customer"<< endl;
cout << endl;
cout<<setw(15)<<"Category"<<setw(19)<<"Weight (lbs.) "<< "Shipping" <<endl;
cout << setfill('-')<< setw(50) << "-"<<endl;
cout << setfill(' ');
if (weight == 0)
{ cout << endl << endl;
cout << "Total number of packages: " << count << endl << endl;}
else
{
while (weight != 0)
{
if (weight > 0.0 && weight <= 2.0)
{ category = 1;
shipping = 2.75;
cout<<setw(20)<< category <<setw(15)<<weight<<"$"<< shipping << endl; }
if (weight > 2.0 && weight <= 5.0)
{ category = 2;
shipping = 4.5;
cout<<setw(20)<< category <<setw(15)<<weight<<"$"<< shipping << endl;}
if (weight > 5.0 && weight <= 8.0)
{ category = 3;
shipping = 6.75;
cout<<setw(20)<< category <<setw(15)<<weight<<"$"<< shipping << endl;}
if (weight > 8.0 && weight <= 10.0)
{ category = 4;
shipping = 8.25;
cout<<setw(20)<< category <<setw(15)<<weight<<"$"<< shipping << endl;}
if (weight <= min && weight != 0)
min = weight;
if (weight >= max)
max = weight;
sum += weight;
total += shipping;
count++;
cin >> weight;
}
cout << endl << endl;
cout << "Total number of packages: " << count << endl;
double average = sum / count ;
cout << "Average weight of all packages: " << average << endl;
cout << "Total shipping charges: " << "$" << total << endl;
cout << "Lightest weight: " << min << endl;
cout << "Heaviest weight: " << max << endl << endl << endl;
cout << "Histogram of category counts: " << endl;
cout << setfill('-')<< setw(33) << "-"<<endl;
cout << setfill(' ');
for (; category == 1;)
cout << "*";
}
system("pause");
return 0;
}
Last edited on Oct 14, 2012 at 4:07am UTC
Oct 13, 2012 at 9:01am UTC
You dont need for loop and the second while loop.
You are already reading weights you dont need to read them allover again.
Just initialise the sum before the first while loop, add weights to the sum in the loop, calculate the average after the loop and print.
Oct 14, 2012 at 4:14am UTC
codewalker thank you very much for your response, it was really helpful and I fixed the problem. I'm currently trying to display a histogram listing the category and one star for each package. It looks like this:
Histogram of category counts:
-------------------------------------------
1 **
2 ***
3 *
4 ***
I'm using a for loop, but it never stops running, and I'm not sure why. I'm using this type of loop:
for (; category == 1;)
cout << "*";
This is the last part of the program that I'm writing. Also, I edited the code above to the current improved version that I'm using. I appreciate your help.
Last edited on Oct 14, 2012 at 4:15am UTC