Help on programming exercise with if else statements

The Fast Freight Shipping Company charges the following rates:
Weight of Package Rate per Pound
2 pounds or less $1.10
Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80
Design a program that asks the user to enter the weight of a package and then displays the shipping charges.

So we have to include modules in the program. There's an error with the logical operators and an error at the "else" statements saying "expected a ;"
Can someone please help me with this program and give any tips?

#include <iostream>
#include <string>
using namespace std;

//Function declarations
double getPackageWeight();
double calculateShippingRates(double weight);
void displayShippingCharges(double shippingRates);

int main()
{
//Declare variables
double weight;
double shippingCharges1, shippingCharges2, shippingCharges3, shippingCharges4;
double const RATE1 = 1.10;
double const RATE2 = 2.20;
double const RATE3 = 3.70;
double const RATE4 = 3.80;
weight = getPackageWeight();

if (weight <= 2){
shippingCharges1 = weight* RATE1;
cout << "Shipping charge is $" << shippingCharges1 << endl;
}
else if (weight > 2)||(weight <= 6){
shippingCharges2 = weight * RATE2;
cout << "Shipping charge is $" << shippingCharges2 << endl;
}
else if (weight > 6) || (weight <= 10){
shippingCharges3 = weight * RATE3;
cout << "Shipping chage is $" << shippingCharges3 << endl;
}
else if (weight > 10){
shippingCharges4 = weight * RATE4;
cout << "Shipping charge is $" << shippingCharges4 << endl;
}
system("pause");
return 0;
}

//Module to get weight of package in pounds
double getPackageWeight()
{
double weight;
cout << "Enter the weight of the package (in pounds):" << endl;
cin >> weight;
return weight;
}
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

The errors you are getting are because you forgot to wrap round parenthesis around the conditions. Right now you only have round parenthesis around the individual parts, but you need an additional set around the whole thing.
Thank you I figured it out shortly after I posted this. My only other question is how would I implement the if else in module form?
slim1214 wrote:
how would I implement the if else in module form?
I don't understand - what is "module form"? I don't believe I have heard of this.
I believe he means modular programming. Kinda sorta separating functions in terms of what they do, I guess. At least that's the only thing that comes to mind when @OP says "module".
Last edited on
closed account (48T7M4Gy)
Do something along these lines for your function/module as you describe it:

You'll need to tidy it up and debug it ...


In main()

1
2
int charge = calculateCharge( int weight);
cout << "Shipping charge is $" << charge << endl;



then separately as you did with your other function getPackageWeight ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int  calculateCharge( int weight)
{

if (weight <= 2){
shippingCharges1 = weight* RATE1;
}
else if (weight > 2)||(weight <= 6){
shippingCharges2 = weight * RATE2;

}
else if (weight > 6) || (weight <= 10){
shippingCharges3 = weight * RATE3;

}
else if (weight > 10){
shippingCharges4 = weight * RATE4;

return shippingCharge

}
Last edited on
Topic archived. No new replies allowed.