Help with IF Else Statements

Sep 8, 2015 at 8:10pm
I currently trying to work on this problem and am running into a problem on how to write it.

Weight of Package Rate per 500 Miles Shipped
2 => weight 1.10
2 < weight <= 6 2.20
6 < weight <=10 3.70
10 < weight <= 20 4.80

So if the user inputs package weight3 pounds, it would charge 1.10 rate * 2.
I am just having a hard time figuring out how I would write the if and if else statement that would factor the following.

1
2
3
4
cout << ("Please enter the weight of the package in pounds: ");
cin >>wPackage
cout << ("Please enter the distance the pack will be shipped in miles: ");
cin >> dShipped


then i followed up with
1
2
3
if (wPackage =>2)
cout<<("Your package weight is: ") wPageage;
cout<<("Your package rate is: ") 


I am trying to figure out where i would put other if statements to calculate the distance. Would I do before it before do the "if" statement for package weight

so
1
2
3

if(dShipped < 500) drate1
if(dShipped > 500 %% dShipped < 1000) drate2


so on and so forth.

As you can see I am slightly confused and need assistance or just some straightening out to make this work.

Thank you all.

DrkMagnus
Sep 8, 2015 at 8:57pm
The problem is unclear to me but you seem to be looking for something like using else if?

http://www.cprogramming.com/tutorial/lesson2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>	

using namespace std;
		
int main()                            // Most important part of the program!
{
  int age;                            // Need a variable...
  
  cout<<"Please input your age: ";    // Asks for age
  cin>> age;                          // The input is put in age
  cin.ignore();                       // Throw away enter
  if ( age < 100 ) {                  // If the age is less than 100
     cout<<"You are pretty young!\n"; // Just to show you it works...
  }
  else if ( age == 100 ) {            // I use else just to show an example 
     cout<<"You are old\n";           // Just to show you it works...
  }
  else {
    cout<<"You are really old\n";     // Executed if no other statement is
  }
  cin.get();
}

Sep 8, 2015 at 10:10pm
I have all this code written, but I keep getting errors with the <= syntax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
	double packageWeight;
	double rate;
	double distance;
	double distanceRate;
	double totalCharge = 0;
	double shippingCharge;
	
	cout << ("Please enter the package weight in pounds: "); packageWeight;
	cin >> packageWeight;
	cout << ("Please enter the distance the package will be shipped in miles: "); distance;
	if (0 < packageWeight %% <= 2){
		rate = 1.10;
	}
	else if (2 < packageWeight && <= 6){
		rate = 2.20;
	}
	else if (6 < packageWeight && <= 10){
		rate = 3.70;
	}
	else if (10 < packageWeight && <= 20){
		rate = 4.80;
	}
	else
		cout << "Invalid package weight" << endl;

	if (0 < distance && <= 500){
		distanceRate = 1;
	}
	else if (500 < distance && <= 1000){
		distanceRate = 2;
	}
	else if (1000 < distance && <= 1500){
		distanceRate = 3;
	}
	else if (1500 < distance && <= 2000){
		distanceRate = 4;
	}
	else
		cout << "Invalid distance" << endl;

	shippingCharge = rate * distanceRate;
	totalCharge += shippingCharge;

	cout << ("Your package will cost you: ") << totalCharge;

	system("pause");
	return 0;


}
Last edited on Sep 8, 2015 at 11:40pm
Sep 9, 2015 at 12:13am
Okay I figured out my error. Now to just figure out why my setprecision(4) is not working in the new code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
	double packageWeight;
	double rate;
	double distance;
	double distanceRate;
	double totalCharge = 0.00;
	double shippingCharge;

	cout << ("Please enter the package weight in pounds: ");
	cin >> packageWeight;
	cout << ("Please enter the distance the package will be shipped in miles: ");
	cin >> distance;
	if (0 == packageWeight){
		rate = 0;
		cout << ("Weight should be greater than 0.");}
	else if (0 < packageWeight && packageWeight <= 2){
		rate = 1.10;}
	else if (2 < packageWeight && packageWeight <= 6){
		rate = 2.20;}
	else if (6 < packageWeight && packageWeight <= 10){
		rate = 3.70;}
	else if (10 < packageWeight && packageWeight <= 20){
		rate = 4.80;}
	else if (20 < packageWeight)
		cout << "MAX Weight is 20 pounds" << endl;

	if (distance < 10){
		cout << ("The company does not ship under 10 miles.");}
	else if (0 < distance && distance <= 500){
		distanceRate = 1;}
	else if (500 < distance && distance <= 1000){
		distanceRate = 2;}
	else if (1000 < distance && distance <= 1500){
		distanceRate = 3;}
	else if (1500 < distance && distance <= 2000){
		distanceRate = 4;}
	else if (2000 < distance && distance <= 2500){
		distanceRate = 5;}
	else if (2500 < distance && distance <= 3000){
		distanceRate = 6;}
	else
		cout << "3000 Miles is the company's max distance" << endl;

	shippingCharge = rate * distanceRate;
	totalCharge += shippingCharge;

	cout << ("Your pageage weight is: ") << packageWeight << endl;
	cout << ("Your shipping distance is: ") << distance << endl;
	cout << ("Your package will cost you: $ ") << std::setprecision(4) << totalCharge << endl;

	system("pause");
	return 0;


}


I need to make it to where the output can be 4 total numbers so XX.XX

but it isnt working the way i want.
Sep 9, 2015 at 12:41am
Make sure to look at Google the method set precision in order to have two digits after dot and two digit before dot.
Topic archived. No new replies allowed.