Baffled on Displaying a List (while, do, for...)

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.
Last edited on
Sounds like you want a vector or a list. You can look those up on this site's reference if you don't know what they are.
Afraid there's nothing in my notes about vectors or lists. I'm pretty sure it has to be done with a loop statement or something similar. while, do, and for statements, and if logic. Also, let me just restate that I have to get the sequence of numbers to come out in the same order in which I put them in.

Look I know beggars can't be choosers but... I gotta be honest, this is an emergency. This assignment is due tomorrow and it's past midnight for me and I had no idea it would be this difficult to figure it out. I started this thing hours ago and I'm still stuck here. I need help desperately.
Last edited on
You can't put a bunch of variables into one variable. You'd need a vector/list or possibly an array.
Oh god I'm freaking out man...
Dude, it's possible. Maybe I'm not using the right terms. Just look at my code and you'll see what I mean. The user has to enter weight values into "PWeight" and then the program has to display all of those values back out again. It has to be possible. This is what I'm supposed to do.
Last edited on
What exactly do you have to average out here? How are you expected to determine the shipping cost? You weren't very clear in stating all of the information there is to work with.
The shipping costs are pre-determined based on weight ranges as listed in the categories up above. For each weight value is a shipping cost, and all of those shipping costs are added together. I have to average out all of the weights entered.

That list format I showed above is my primary concern. I have no idea how to get that to work. I know it has to do with the order in which I do my while statement or something, but that's all I've got.
Last edited on
Please help me... I don't want to take another 20 point hit on another late assignment... I've got literally 90 minutes left to write this whole thing now.
Last edited on
60 minutes...

Well this sucks. That's 20 points off my grade now. I have to go to bed. Seriously, this shouldn't have happened. I started working on this thing hours ago and I couldn't get any further because of this one stupid thing I couldn't figure out how to do that apparently nobody else knows how to do either.

Okay that was mean and I'm sorry. I have no right to complain when I asked you for help. Especially when I did it at half past midnight. Thanks for trying anyway.
Last edited on
You can do this with an array (it's still 1 variable) double PWeight[100] // 100 or whatever is the limit and the totalPackages as an index for the first loop. You need a second loop for the output.

You can make that unlimited when you use dynamic memory...
Topic archived. No new replies allowed.