Re: Shipping charges problem
Just a suggestion.
This,
Code:
if (distance < 501)
// ...
else if (distance >= 501 && distance < 1001)
// ...
is equivalent to this,
Code:
if (distance < 501)
// ...
else if (distance < 1001)
// ...
Re: Shipping charges problem
oh thanks, can you help me with the program termination? because right now if the weight is out of range and the distance is out of range. it will only say "We only ship packages between 10-3000 miles.The shipping charge is 0.00." It doesnt mention the weight and it follows it up with "the shipping charge is 0.00". how can i make it so after the user inputs a weight that is out of range. it will tell the user the weight is out of range and not continue with the program any further?
Re: Shipping charges problem
Quote:
Originally Posted by
Sabensohn70
oh thanks, can you help me with the program termination? because right now if the weight is out of range and the distance is out of range. it will only say "We only ship packages between 10-3000 miles.The shipping charge is 0.00." It doesnt mention the weight and it follows it up with "the shipping charge is 0.00". how can i make it so after the user inputs a weight that is out of range. it will tell the user the weight is out of range and not continue with the program any further?
If you just want to exit the program, then exit will do it.
However, you should avoid calling exit, and use "return", which will only stop the current function. Since your function is main, the effect is the same.
Another option is to do a while loop. If the input is not correct, then you ca call "break" or "continue". They both have the effect of immediately stopping the current iteration of your loop. break will leave the loop entirely, but continue will put you back at the beginning of a new loop.
Hope that helps!
PS, Divide by 500
Your formula is equivalent to:
Code:
shipcharge = 1.10 * ((distance-1)/500)
And this works regardless of distance.
Re: Shipping charges problem
can you show me how to use break and continue in my code please?? also (distance-1/500) because for one of the inputs it is weight 5.0, distance 10 and the desired out put is 2.20.
Re: Shipping charges problem
Quote:
Originally Posted by
Sabensohn70
can you show me how to use break and continue in my code please?? also (distance-1/500) because for one of the inputs it is weight 5.0, distance 10 and the desired out put is 2.20.
PHP Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//Always initialize variables. It is safer
int weight = 0, distance = 0;
float weightCoef = 0, distanceCoef = 0, shipcharge = 0;
while (true) //always loop
{
cout << "Enter the weight of the package(in kg):";
cin >> weight;
if (weight < 0 || weight > 20)
{
cout << "We only accept packages between 1 to 20 kg.";
continue; //try again
}
cout << "Enter the distance to be shipped(in miles):";
cin >> distance;
if (distance < 10 || distance > 3000)
{
cout << "We only ship between 10 to 3000 miles.";
continue; //try again
}
if (weight <= 2)
{
weightCoef = 1.10;
}
else if (weight <= 6) //No need to check if the current weight is higher than 2
{
weightCoef = 2.20;
}
else if (weight <= 10)
{
weightCoef = 3.70;
}
else // (weight > 10 && weight <= 20) No need to check, there is no other option.
{
weightCoef = 4.80;
}
distanceCoef = ((distance -1)/500) + 1; //integer division here
shipcharge = weightCoef*distanceCoef;
cout << showpoint << setprecision(2) << fixed;
cout << "The shipping charge is: " << weightCoef << "*" << distanceCoef << " = " <<shipcharge << endl;
char keepGoing;
cout << "Process another parcel? (Y/N):" << shipcharge << endl;
cin >> keepGoing;
if (keepGoing != 'Y')
{
cout << "Goodbye." << shipcharge << endl;
break; //Leaves the loop
}
}
return 0; //leaves the function
}
You don't have to take my code, but I don't like posting code I wouldn't use myself. Also, about the distance/500 thing: I thought distance was an integer. If you change distance to integer (IMO, you should), then the formula works. If not, you can just use math.h and:
Code:
floor((distance -1)/500) + 1;
Re: Shipping charges problem
Quote:
Originally Posted by
monarch_dodra
PHP Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//Always initialize variables. It is safer
int weight = 0, distance = 0;
float weightCoef = 0, distanceCoef = 0, shipcharge = 0;
while (true) //always loop
{
cout << "Enter the weight of the package(in kg):";
cin >> weight;
if (weight < 0 || weight > 20)
{
cout << "We only accept packages between 1 to 20 kg.";
continue; //try again
}
cout << "Enter the distance to be shipped(in miles):";
cin >> distance;
if (distance < 10 || distance > 3000)
{
cout << "We only ship between 10 to 3000 miles.";
continue; //try again
}
if (weight <= 2)
{
weightCoef = 1.10;
}
else if (weight <= 6) //No need to check if the current weight is higher than 2
{
weightCoef = 2.20;
}
else if (weight <= 10)
{
weightCoef = 3.70;
}
else // (weight > 10 && weight <= 20) No need to check, there is no other option.
{
weightCoef = 4.80;
}
distanceCoef = ((distance -1)/500) + 1; //integer division here
shipcharge = weightCoef*distanceCoef;
cout << showpoint << setprecision(2) << fixed;
cout << "The shipping charge is: " << weightCoef << "*" << distanceCoef << " = " <<shipcharge << endl;
char keepGoing;
cout << "Process another parcel? (Y/N):" << shipcharge << endl;
cin >> keepGoing;
if (keepGoing != 'Y')
{
cout << "Goodbye." << shipcharge << endl;
break; //Leaves the loop
}
}
return 0; //leaves the function
}
You don't have to take my code, but I don't like posting code I wouldn't use myself. Also, about the distance/500 thing: I thought distance was an integer. If you change distance to integer (IMO, you should), then the formula works. If not, you can just use math.h and:
Code:
floor((distance -1)/500) + 1;
I'd change while(true) to while(keepGoing == 'Y')
and I'd wrap the distance check in a while statement too since the continue if the distance isn't correct will require the user to reenter the weight.
Also you want to check weight < 1, not < 0.