So I have this assignment. The user must enter weights of various packages one right after the other, separated by a space each. When the user is done entering these weights, the user presses 0 to end the sequence and hits enter. All of these values are then put into a single variable.
Then, I must average these values out, and put them into categories. The categories are:
1 - weight is greater than 0 but less than 2 (shipping is 2.75)
2 - weight is greater than 2 but less than 5 (shipping is 4.50)
3 - weight is greater than 5 but less than 8 (shipping is 6.75)
4 - weight is greater than 8 but less than 10 (shipping is 8.25)
At the end of all of this, I've got to display something in a very organized way with the weights. So using the example weights 1.0, 2.4, 3.4, 0.3, 5.7, 4.7, 9.9, the output would have to look like this.
Category Weight Shipping
1..............1.00.......etc.
2..............2.40.......etc.
2..............3.40
1..............0.30
3 .............5.70
2..............4.70
4..............9.90
Now, I am absolutely baffled as to how I can make all of those values (packed into one variable, mind you) display like that, along with categories automatically fit next to each one, and shipping. What's more, the program needs to be able to do this for an unlimited amount of values.
And it's got to be done using while, do, and for statements, and if logic. Nothing more advanced than that. The program also needs to ignore invalid data entries. (I guess those are like, weights in excess of 10 pounds, or letters).
So what I'm asking is, can I put a bunch of values into one variable, and then pull them out in the same order they were entered, attach a category to each one, and attach a shipping cost to each one? And how can I tell the program to ignore invalid data and keep going? Any help is appreciated.
This 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 32 33 34
|
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double PWeight; // Package weights. ("P" for Package).
double sumPWeight = 0; // Sum of weights.
int totalPackages = 0; // Number of weights.
double average; // Average of weights.
int category; // Category weights fit into.
cout << "Enter package weights (0 to quit): ";
cin >> PWeight; // First weight value.
while (PWeight != 0)
{
totalPackages++; // Counting the values.
sumPWeight = sumPWeight + PWeight;
cin >> PWeight; // Additional weight values.
}
cout << "Weight Statistics" << endl;
cout << "\nCategory Weight (lbs.) Shipping" << endl;
cout << "-----------------------------------" << endl;
if (totalPackages > 0)
{
average = sumPWeight / totalPackages;
}
return 0;
}
|
PWeight is the variable I'm putting the weights into and then pulling them from.