In: Computer Science
Programming 3: Multi-Way Branching
Shipping Charges
The Fast Freight Shipping Company charges the following rates:
Given the Weight of Package (in Kilograms), use the following Rate ($) per 500 Miles Shipped.
2 kg or less = $1.10 per 500 Miles Shipped
Over 2 kg but not more than 6 kg = $2.20 per 500 Miles Shipped
Over 6 kg but not more than 10 kg = $3.70 per 500 Miles Shipped
Over 10 kg but not more than 20 kg = $4.80 per 500 Miles Shipped
Write a program that reads the weight of the package and the distance it is to be shipped, and then displays the charges to two decimal points.
I got stuck here:
#include
using namespace std;
int main(){
int i=0,miles;
   double j,weight,cost,distance;
   cin>>weight;
      if(weight<=0){
         cout<<
"ILLEGAL WEIGHT: BELOW MINIMUM" << endl;
      }
      else(weight>20){
         cout<<
"ILLEGAL WEIGHT: ABOVE MAXIMUM" << endl;
      }
       
   cin>>distance;
      while(distance<10 || distance
>3000){
         cout<<
"ILLEGAL DISTANCE" << endl;
      }
   i=miles/500;
   j=miles%500;
   if(j>0){
       i=i+1;
   }
   if(weight<=2){
       cost=i*1.10;
   }
   else if(weight>2 && weight<=6){
       cost=i*2.20;
   }
   else if(weight>6 && weight<=10){
       cost=i*3.70;
   }
   else if(weight>10 && weight<=20){
       cost=i*4.80;
   }
   cout<<"Cost of Shipping: " << cost;
   return 0;
}
Random inputs are....
Input : -1 2000
Expected inputs are : ILLEGAL WEIGHT: BELOW MINIMUM
Input: 1 5000
Expected output : ILLEGAL DISTANCE
input: 2 2000
Expected output : 4.40 (<--| enter symbol)
Please revise and let me know what i did wrong.....
Corrected Program
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    int i=0;
    double j,weight,cost,distance;
    cin>>weight;
      if(weight<=0)
      {
          cout<<
"ILLEGAL WEIGHT: BELOW MINIMUM" << endl;
          return
0;
       }
       if(weight>20){
          cout<<
"ILLEGAL WEIGHT: ABOVE MAXIMUM" << endl;
          return
0;
       }
    cin>>distance;
       if((distance<10
)||(distance >3000))
    {
          cout<<
"ILLEGAL DISTANCE" << endl;
          return
0;
    }
    i=distance/500;
    j=int(distance)%500;
    if(j>0)
    {
        i=i+1;
    }
    i=float(i);
    if(weight<=2){
        cost=i*1.10;
    }
    else if((weight>2) &&
(weight<=6))
        {
cost=i*2.20;
    }
    else if((weight>6 )&&
(weight<=10)){
        cost=i*3.70;
    }
    else if((weight>10 )&&
(weight<=20)){
        cost=i*4.80;
    }
    cout<<"Cost of Shipping: "
<<setprecision(2)<< cost;
    return 0;
}