This is a starter:
Code:
#include <iostream>
using namespace std;
//Kg = weight in Kilogram
static const double R1 = 1.10; // 0 < Kg <= 2
static const double R2 = 2.20; // 2 < Kg <= 6
static const double R3 = 3.70; // 6 < Kg <= 10
static const double R4 = 4.80; // 10 < Kg <= 20
//Distance step (in miles)
static const unsigned DIST_STEP = 500;
int main ()
{
double wt_package;
cout << "Please enter the Wt. Package (Kg): ";
cin >> wt_package;
while(wt_package <=0 || wt_package > 20)
{
cout<<"ERROR: Weight too small or too big. Please enter the weight: ";
cin>>wt_package;
}
double distance;
cout << "Please enter the distance (miles): ";
cin >> distance;
while(distance < 10 || distance > 3000)
{
cout<<"ERROR: distance too small or to big. Please enter the distance: ";
cin>>distance;
}
//Determine the shipping rate based on weight.
//Assumes input validation took place.
double wt_rate;
wt_package <= 2 ? wt_rate=R1 : 0;
wt_package > 2 && wt_package <= 6 ? wt_rate=R2 : 0;
wt_package > 6 && wt_package <= 10 ? wt_rate=R3 : 0;
wt_package > 10 ? wt_rate = R4 : 0;
//Determine the shipping rate based on distance.
unsigned dist_rate = static_cast<unsigned>(distance/DIST_STEP);
//Calculate the total cost.
double total_cost = dist_rate * wt_rate;
//Display the total cost.
cout << total_cost <<endl;
//Prevent console flashing.
std::cin.get(); //Optional.
}