View Single Post
Old 09-25-2005, 08:36 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
  
}
__________________
Valmont is offline   Reply With Quote