|
-
October 26th, 2009, 11:44 PM
#1
Shipping charges problem
Hi guys I am suppose to write a program that asks for the weight of the package and the distance it is to be shipped and then displays the charges. I pretty much got every down except i dont know how to make the program terminate after line 12 if the weight isnt in the right range, terminate after line 17 if the distance isnt in the right range. For example if the weight is 21, I want it to just say "We only accept packages between 1 to 20 kg." and the program stops without going any further. Please help!
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float weight, distance, rate, shipcharge;
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.";
else
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.";
if (weight <= 2)
{
if (distance < 501)
shipcharge = 1.10 * 1;
else if (distance >= 501 && distance < 1001)
shipcharge = 1.10 * 2;
else if (distance >= 1001 && distance < 1501)
shipcharge = 1.10 * 3;
else if (distance >= 1501 && distance < 2001)
shipcharge = 1.10 * 4;
else if (distance >= 2001 && distance < 2501)
shipcharge = 1.10 * 5;
else if (distance >= 2501 && distance <= 3000)
shipcharge = 1.10 * 6;
}
else if (weight > 2 && weight <= 6)
{
if (distance < 501)
shipcharge = 2.20 * 1;
else if (distance >= 501 && distance < 1001)
shipcharge = 2.20 * 2;
else if (distance >= 1001 && distance < 1501)
shipcharge = 2.20 * 3;
else if (distance >= 1501 && distance < 2001)
shipcharge = 2.20 * 4;
else if (distance >= 2001 && distance < 2501)
shipcharge = 2.20 * 5;
else if (distance >= 2501 && distance <= 3000)
shipcharge = 2.20 * 6;
}
else if (weight > 6 && weight <= 10)
{
if (distance < 501)
shipcharge = 3.70 * 1;
else if (distance >= 501 && distance < 1001)
shipcharge = 3.70 * 2;
else if (distance >= 1001 && distance < 1501)
shipcharge = 3.70 * 3;
else if (distance >= 1501 && distance < 2001)
shipcharge = 3.70 * 4;
else if (distance >= 2001 && distance < 2501)
shipcharge = 3.70 * 5;
else if (distance >= 2501 && distance <= 3000)
shipcharge = 3.70 * 6;
}
else if (weight > 10 && weight <= 20)
{
if (distance < 501)
shipcharge = 4.80 * 1;
else if (distance >= 501 && distance < 1001)
shipcharge = 4.80 * 2;
else if (distance >= 1001 && distance < 1501)
shipcharge = 4.80 * 3;
else if (distance >= 1501 && distance < 2001)
shipcharge = 4.80 * 4;
else if (distance >= 2001 && distance < 2501)
shipcharge = 4.80 * 5;
else if (distance >= 2501 && distance <= 3000)
shipcharge = 4.80 * 6;
}
cout << showpoint << setprecision(2) << fixed;
cout << "The shipping charge is:" << shipcharge << endl;
return 0;
}
-
October 27th, 2009, 01:30 AM
#2
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)
// ...
-
October 27th, 2009, 02:33 AM
#3
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?
-
October 27th, 2009, 03:30 AM
#4
Re: Shipping charges problem
 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.
-
October 27th, 2009, 04:15 AM
#5
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.
-
October 27th, 2009, 05:24 AM
#6
Re: Shipping charges problem
 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;
-
October 27th, 2009, 05:50 AM
#7
Re: Shipping charges problem
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|