Need help with a program.

closed account (D4NbpfjN)
The Fast Freight Shipping Company charges following rates
Weight of Packages (pounds) Rate per 500 miles Shipped
2 => weight $1.10
2 < weight <= 6 $2.20
6 < weight <= 10 $3.70
10 < weight <= 20 $4.80
Write a C++ program that asks for the weight of the package and the distance to be shipped. Then, compute the total charge. Charge is computed in 500 mile increments. For example, if the weight of the package is 3 pounds and distance to travel is 680 miles, then the charge will be
$2.20 * 2 = $4.40

Do not accept 0 or less for the weight of the package. If the weight of the package is 0 or less, display
the message “weight should be a greater than 0”
Do not accept more than 20 for the weight of the package. If the weight of the pages more than 20, display the message “max weight should 20”
Also, do not accept the distances of less than 10 and greater than 3000 (these are the company’s min
and max shipping distances)

This is the code I have so far. What do I need to do from here?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<iomainp>
using namespace std;

int main()

{
	const int distance_unit = 500;
	double rate = 0, shippingcost;
	int weight, distance

		cout << setprecision(2) << fixed;

	cout << "How much does the package weigh in kilograms?";
	cin >> weight;

	if(weight <= 0)
	{
		cout << "The weight should be greater than zero";
	}
	if (weight > 20)
	{
		cout << "The max weight should be 20"; }
	
	else   

	cout << "How far will the package be going?";
	cin >> distance;

	if (distance < 10 || distance > 3000)
	{
		cout << "The distance you entered is not in the min and max range."; }

	if (weight <= 2)
	{
	rate = 1.10;
	}
	else if (weight >2 && weight <= 6)
	{
	rate = 2.20;
	}
	else if (weight > 6 && weight <= 10)
	{
	rate = 3.70;
	}
	else
	{
	rate = 4.80;
	}

	shippingcost = distance * rate;







Line 17: If the weight is negative, you display a message, then continue on as if the value were good.

Line 21 Ditto for weight > 20.

Line 30: Ditto for distance out of range.

Hint: Use a while loop to continue prompting until input is valid.

Line 27: The cout is subject to the else

Line 51: distance is charged in multiples of 500 miles. I see no calculation of the multiple. Use the modulo operator to determine multiples of 500 miles.

Line 52: Your program is missing after this point. You need to display shipping cost, return 0 and have a }

Last edited on
closed account (D4NbpfjN)
So this is my program so far. The only thing i am having trouble with is that its not calculating like the example shows above. If i have a package that weighs 3 pounds and the distance is 680 then it would be 2.20*2=4.40. How do i incorporate this into my program?


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include <iomanip> 
using namespace std;

int main()

{
	const int distance_unit = 500;
	double rate = 0, shippingcost;
	int weight, distance, Distanceunit;

		cout << setprecision(2) << fixed;

	cout << "How much does the package weigh in kilograms?";
	cin >> weight;

	if(weight <= 0)
	{
		cout << "The weight should be greater than zero\n";
	}
	if (weight > 20)
	{
		cout << "The max weight should be 20"; }
	
	else   

	cout << "How far will the package be going?";
	cin >> distance;

	if (distance < 10 || distance > 3000)
	{
		cout << "The distance you entered is not in the min and max range."; }
	
	Distanceunit = distance / distance_unit;
	if (distance%distance_unit != 0)

	if (weight <= 2)
	{
	rate = 1.10;
	}
	else if (weight >2 && weight <= 6)
	{
	rate = 2.20;
	}
	else if (weight > 6 && weight <= 10)
	{
	rate = 3.70;
	}
	else
	{
	rate = 4.80;
	}

	shippingcost = Distanceunit * rate;
	cout << "The cost to ship a package that weighs " << weight
		<< " Kilograms\nfor a distance of " << distance
		<< " miles is $" << shippingcost << endl << endl;

	return 0;
}
Line 14,56: The instructions state the weight is in pounds. You're asking for kilograms.

Lines 17,21,30: My comments from before still apply.

Line 34: You should be adding 1 to the calculation. If the distance is less than 500, you're going to get a Distanceunit of 0.

Line 35: You don't need the modulo operator here. Sorry my advice to use the modulo operator was incorrect. Simple division is all you need. If line 35 is false, you never change rate, which was initialized to 0. Actually, this if statement is unnecessary.
 
Distanceunit = (distance / distance_unit) + 1;






Last edited on
Topic archived. No new replies allowed.