rounding and input validation

2kgs or less -- rate per 500 miles shipped = $1.10
upto or 6kg -- rate per 500 miles shipped = $1.10
upto or 10kg-- rate per 500 miles shipped = $1.10
upto 20kg-- rate per 500 miles shipped = $1.10

input validations
distance < 10 and > 3000 miles
weight cannot be 0 and should be less than 20 kgs

ask user the distance and the weight of the package.

I am facing these problems
1) how do i round the amount to two decimals?
2) input validation does not work.

Posting my code below this please help me.
Thanks :)
// Hasnain Attarwala
// Lab3 problem 13
// Page 222
// fast freight

#include <iostream>;
#include <iomanip>;
#include <string>;
#include <cmath>;


using namespace std;

int main ()
{
// declare variables
float weight, rate, distance, rpm, cost;






// Input
weight = 0;
cout << "Enter weight" << endl;
cin >> weight;

while ( weight < 0 && weight > 20)
{

cout << "please enter a weight less than 20 and greater than 0 in kgs" << endl;
cout << "Enter weight: ";
cin >> weight;
}

distance = 0;
cout << "Enter distance" << endl;
cin >> distance;

while ( distance < 10 && distance > 3000)
{
cout << "please enter a distance less than 3000m and greater than 10 miles" << endl;
cout << "Enter distance: ";
cin >> distance;
}

// decide rate
if (weight <= 2)
cost = 1.10;
else if (weight < 6)
cost = 2.20;
else if (weight < 10)
cost = 3.70;
else cost = 4.80;

// math
rpm = cost * distance;
rate = (rpm / 500);

// output

cout << "And your total comes up to be..... " << endl << "$" << rate << endl;



return 0;

}
Topic archived. No new replies allowed.