How can I go about writing this loop?

I'm just learning C++ so bare with me. How can I loop the input by the user to handle multiple packages until a weight of zero is entered? The loop should also calculate the total. Thanks in advance guys.

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
35
#include <iostream>
#include <cstdlib>
using namespace std;

void calculateCharge(int weight, int miles)
{
	cout << "The cost to ship these items will be: " << endl;
}

int main()
{
	int kilograms;
	int distance;
	double cost;
	
	
	
		cout << "Please enter the weight of the package" << endl;
		cin >> kilograms;
		cout << "Please enter the distance of shipment" << endl;
		cin >> distance;
		
	calculateCharge(kilograms, distance);

	if (kilograms <= 2)
		cost = 3.10 * (distance / 500);
	else if (kilograms >= 3 || kilograms <= 6)
		cost = 4.20 * (distance / 500);
	else if (kilograms >= 7 || kilograms <= 10)
		cost = 5.30 * (distance / 500);
	else if (kilograms > 10)
		cost = 6.40 * (distance / 500);

	return 0;
}
Last edited on
Using && (and) operator not (||) operator (or).
1
2
3
4
5
6
7
8
	if (kilograms <= 2)
		cost = 3.10 * (distance / 500);
	else if (kilograms >= 3 || kilograms <= 6)
		cost = 4.20 * (distance / 500);
	else if (kilograms >= 7 || kilograms <= 10)
		cost = 5.30 * (distance / 500);
	else if (kilograms > 10)
		cost = 6.40 * (distance / 500);


Should be :
1
2
3
4
5
6
7
8
	if (kilograms <= 2)
		cost = 3.10 * (distance / 500);
	else if (kilograms >= 3 && kilograms <= 6)
		cost = 4.20 * (distance / 500);
	else if (kilograms >= 7 && kilograms <= 10)
		cost = 5.30 * (distance / 500);
	else if (kilograms > 10)
		cost = 6.40 * (distance / 500);

Have a look at the do-while loop.
http://www.cplusplus.com/doc/tutorial/control/

For example:
1
2
3
4
5
6
7
8
do
{
		cout << "Please enter the weight of the package" << endl;
		cin >> kilograms;
		total = total + kilograms;
}
while (kilograms != 0);
cout << "Please enter the distance of shipment" << endl;
I fixed the loop although once I enter zero for kilograms and distance, I'm not getting the right calculations. When I enter zero for both, i'm getting an integer and not a dollar amount. I think its displaying total for the items I entered. Here's my new code.

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
int kilograms = 0;
	int distance = 0;
	double cost = 0;
	
	
	
	do
	{
		cout << "Please enter the weight of the package" << endl;
		cin >> kilograms;
		cout << "Please enter the distance of shipment" << endl;
		cin >> distance;
		cost = cost + kilograms;
	} 
		while (kilograms !=0);
	cout << "Your total to ship these item(s) will be: " << total << endl;
		
	calculateCharge(kilograms, distance);

	if (kilograms <= 2)
		cost = 3.10 * (distance / 500);
	else if (kilograms >= 3 && kilograms <= 6)
		cost = 4.20 * (distance / 500);
	else if (kilograms >= 7 && kilograms <= 10)
		cost = 5.30 * (distance / 500);
	else if (kilograms > 10)
		cost = 6.40 * (distance / 500);

	return 0;
Topic archived. No new replies allowed.