Need Help With If Statement Program!

Hello, I am struggling with getting the correct output from my program, here is the problem followed by my code:

Weight of Package (in Kilograms) | Rate per 500 Miles shipped

2 KG or less | $1.10

Over 2 Kg but not more than 6 Kg | $2.20

Over 6 KG but not more than 10 Kg | $3.70

Over 10 KG but not more than 20 KG | $4.80

Write a program that asks for the weight of the package and the distance it is to be shipped, and then displays the charges.

Input validation: Do not accept values of 0 or less for the weight of the package.Do not accept weights of more than 20 Kg (this is the maximum weight the company will ship). Do not accept distances of less than 10 miles or more than 3,000 miles.
These are the company’s minimum and maximum shipping distances.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double packageWeight, packageDistance, miles, rate, shippingTotal,distanceUnit;//Declaring my variables
	
	cout<<setprecision(2)<<fixed;
	
	cout<<"Enter the weight of your package (kilograms)"<<endl;//Prompt user to input weight
	cin>>packageWeight;//Store weight in packageWeight
	
	if (packageWeight<=0||packageWeight>20)//Values of 0 or less and more than 20 are not accepted
	{
	cout<<"I'm sorry, the weight you entered cannot be accepted"<<endl;
	}	
	
	cout<<"Enter the distance your package is being shipped (miles)"<<endl;//Prompt user to input distance
	cin>>packageDistance;//Store it in packageDistance
	
	if(packageDistance<10||packageDistance>3000)
	{
	cout<<"I'm sorry, the distance you entered cannot be accepted"<<endl;//Distances less than 10 and more than 3000 are not accepted
	return 0;
	}
	
	if (packageWeight<=2)
	{
	rate=1.10;
	}
	else if(packageWeight>2&&packageWeight<=6)
	{
	rate=2.20;
	}
	else if (packageWeight>6&&packageWeight<=10)
	{
	rate=3.70;
	}
	else if (packageWeight>10&&packageWeight<=20)
	{
	rate=4.80;
	}
	if (miles > 10 && miles < 500)
	{
	miles = 1;
	}
	else if (miles > 500 && miles < 1000)
	{
	miles = 2;
	}
	else if (miles > 1000 && miles < 1500)
	{
	miles = 3;
	}
	else if (miles > 1500 && miles < 2000)
	{
	miles = 4;
	}
	else if (miles > 2500.0 && miles <= 3000)
	{
	miles = 5;
	}
	
	shippingTotal=packageDistance*rate*miles;
	cout<<"The total for your shipping charges are: $"<<shippingTotal<<endl;
	
	return 0;
}
Last edited on
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
#include <iostream>
#include <iomanip>

int main()
{
    const int MAX_WEIGHT = 20 ;
    const int MIN_DISTANCE = 10 ;
    const int MAX_DISTANCE = 3'000 ;
    const int SHIPPING_RATE_MILES = 500 ;

    std::cout << "Enter the weight of your package (kilograms): " ;
    int weight ;
    std::cin >> weight ;

    if( weight <= 0 || weight > MAX_WEIGHT )
    {
        std::cerr << "I'm sorry, the weight you entered cannot be accepted\n" ;
        return 1 ;
    }

    std::cout << "Enter the distance your package is being shipped (miles): " ;
    int distance ;
    std::cin >> distance ;

    if( distance < MIN_DISTANCE || distance > MAX_DISTANCE )
    {
        std::cerr << "I'm sorry, the distance you entered cannot be accepted\n" ;
        return 1 ;
    }

    // compute shipping rate per SHIPPING_RATE_MILES (500 miles)
    double rate = 4.80 ; // initialised for 10 kg or more
    if( weight <= 2 ) rate = 1.10 ; // 2 kg or less
    else if( weight <= 6 ) rate = 2.20 ; // over 2 Kg but not more than 6 Kg
    else if( weight <= 10 ) rate = 3.70 ; // over 6 Kg but not more than 10 Kg
    // else over 10 Kg, we had already initialised it for over 10 Kg

    // compute the number of SHIPPING_RATE_MILES (500 mile) segments
    int num_segments = (distance-1) / SHIPPING_RATE_MILES + 1 ;
    // distance 237: (237-1)/500 + 1 == 1
    // distance 500: (500-1)/500 + 1 == 1
    // distance 501: (501-1)/500 + 1 == 2
    // etc.

    // compute the shipping charge
    const double charge = rate * num_segments ;

    std::cout << "The shipping charges are: $" << std::fixed
              << std::setprecision(2) << charge << '\n' ;
} 
Topic archived. No new replies allowed.