if/ else if statements

This is for a CS homework assignment. When I run this program, for the first cout statement that says: "\nUsing logical operators to identify ranges:\n"... it displays 0.00 but the second one displays a correct number. How do I make the program stop after the first set of if/ else if statements and start over for the second if/ else statements?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include<iomanip>
using namespace std;

/*This program gets the weight of a package and
the distance it is to be shipped. Then it computes
the shipping charges for that package.
*/
int main() {
	const int DISTANCE_UNIT = 500;
	double rate = 0, shippingCost;
	int weight, distance, distanceUnits;

	cout << setprecision(2) << fixed;

	cout << "How much does the package weigh (in Kilograms)? ";
	cin >> weight;

	if (weight > 20 || weight <= 0)
	{
		cout << "Fast Freight Shipping does not ship packages with a weight of " << weight << " kg." << endl;
		return 0;
	}
	//add code to verify weight is allowed

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

	if (distance < 10 || distance > 3000)
	{
		cout << "Fast Freight Shipping does not ship packages traveling a distance of " << distance << " miles." << endl;
		return 0;
	}
	//add code to verify distance is allowed

	distanceUnits = distance / DISTANCE_UNIT;
	if (distance%DISTANCE_UNIT != 0)
		distanceUnits += 1;

	//deteremine rate using if/else if with 
	//complex conditions the use 
	//logical operators to identify ranges

	shippingCost = distanceUnits * rate;

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

	cout << "\nUsing logical operators to identify ranges:\n"
		<< "The cost to ship a package that weighs " << weight
		<< " Kilograms\nfor a distance of " << distance
		<< " miles is $" << shippingCost << endl << endl;

	//deteremine rate using if/else if with simple conditions
	//using order to correctly separate the options

	
	shippingCost = distanceUnits * rate;
	

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

	cout << "Using the order of the if/else if statement:\n"
		<< "The cost to ship a package that weighs " << weight
		<< " Kilograms\nfor a distance of " << distance
		<< " miles is $" << shippingCost << endl << endl;

	return 0;
}

Your question is unclear to me, however the reason it's displaying 0.00 is because you initialize rate to 0, then you do the calculation - shippingCost = distanceUnits * rate; when it's still 0, you change it after.
Topic archived. No new replies allowed.