confusion with primary expression if/else if statements

This is only part of the program but I can't get my nested if/ else if statements to work correctly. I wrote a line that tests to make sure the user entered a valid choice. Then I put an if choice = 1 then a block of statements based on what needs to happen for that choice. All worked well until I added and else if to test if the choice = 2. The compiler then says that an "if" is needed before the "else". I don't get it. Thank you.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #include <iostream>
#include <iomanip> //THis is to format the outputs. 
#include <cstdlib> //This is for generating random zip codes. 
using namespace std;

int main()
{
	//Variables for string input
	string name, street, city, state;
	// Variables for the zip code math. 
	int zip, check,
		ones,
		tens, tensA, 
		hund, hundA, 
		thou, thouA, 
		tenK, tenKA, 
		sum;
	//Variables for Postage rates
	int package;
	double weight, postage;
	//These are the shipping rates
	const double 	LETTER = 0.49,
					LETTERplus = 0.22,
					ENVELOPE = 0.98,
					ENVELOPEplus = 0.22,
					PARCEL = 2.54,
					PARCELplus = 0.20;
	
	//This section informs the user of the program's intent. 
	//It then prompts the user for their information. 
	
	cout << "Hello, User. This program will generate ";
	cout << "a mailing label and the cost to ship an item. \n";
	cout << "Please enter the following information.\n";
	cout << "What is your name? ";
	getline(cin, name);
	cout << "What is your street address? ";
	getline(cin, street);
	cout << "What is your city? ";
	getline(cin, city);
	cout << "What is your state? ";
	getline(cin, state);
	cout << "What is your zip code? ";
	cin >> zip;
	cout << "What type of item are you shipping?\n";
	cout << "1. Letter\n";
	cout << "2. Large envelope\n";
	cout << "3. Parcel\n";
	cin >> package;
	cout << "What is the weight in ounces? ";
	cin >> weight;
	cout << "\n";
	
	//The next line formats the output
	cout << fixed << showpoint << setprecision(2);
	
	//This part of the program calculates shipping charges. 
	if (package != 1 && package != 2 && package != 3)
		cout << "Please choose a valid package.";
	if (package == 1)
		{if (weight < 1.0 && weight > 0.0)
			postage = LETTER;
		}
		{if (weight > 1.0)
			postage = (LETTER + (weight * LETTERplus));
		}
	else if (package == 2)
		{if (weight < 1.0 && weight >0.0)
			postage = ENVELOPE;
		}
		{if (weight > 1.0)
			postage = (ENVELOPE + (weight * ENVELOPEplus));
		}
	else if (package == 3)
		{if (weight < 1.0 && weight > 0.0)
			postage = PARCEL;
		}
		{if (weight > 1.0)
			postage = (PARCEL + (weight * PARCELplus));
		}
	
	
To use an if/else combination, the else has to be the first thing after the block of the if statement. In the section of your code below, I have put a comment at the location where the else would have to go.

1
2
3
4
5
6
7
8
	if (package == 1)
		{if (weight < 1.0 && weight > 0.0)
			postage = LETTER;
		} // HERE IS THE END OF THE LINE 1 IF BLOCK
               // THE ELSE WOULD HAVE TO GO RIGHT HERE WHERE THIS COMMENT IS
               {if (weight > 1.0)
			postage = (LETTER + (weight * LETTERplus));
		}


You could fix it like this:

1
2
3
4
5
6
7
8
9
10
	if (package == 1)
       {
		{if (weight < 1.0 && weight > 0.0)
			postage = LETTER;
		}
		{if (weight > 1.0)
			postage = (LETTER + (weight * LETTERplus));
		}
        }  // HERE IS THE END OF THE IF BLOCK
        else if...

Last edited on
Thank you! I'm still working on it but your reply has helped.
I keep getting a postage value of 0.00 but it seems that the if/else statements are working now. I can't figure out why the postage value becomes zero though.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	//Variables for Postage rates
	int package;
	double weight, postage;
	
	
	//This section informs the user of the program's intent. 
	//It then prompts the user for their information. 
	
	cout << "Hello, User. This program will generate ";
	cout << "a mailing label and the cost to ship an item. \n";
	cout << "Please enter the following information.\n";
	cout << "What is your name? ";
	getline(cin, name);
	cout << "What is your street address? ";
	getline(cin, street);
	cout << "What is your city? ";
	getline(cin, city);
	cout << "What is your state? ";
	getline(cin, state);
	cout << "What is your zip code? ";
	cin >> zip;
	cout << "What type of item are you shipping?\n";
	cout << "1. Letter\n";
	cout << "2. Large envelope\n";
	cout << "3. Parcel\n";
	cin >> package;
	if (package != 1 && package != 2 && package != 3)
		{	cout << "Please enter a valid item type.";
		return 0;
		}
	cout << "What is the weight in ounces? ";
	cin >> weight;
	cout << "\n";
	
	//The next line formats the output
	cout << fixed << showpoint << setprecision(2);
	
	//This part of the program calculates shipping charges.
	 //These are the shipping rate variables
	const double 	LETTER = 0.49,
					LETTERplus = 0.22,
					ENVELOPE = 0.98,
					ENVELOPEplus = 0.22,
					PARCEL = 2.54,
					PARCELplus = 0.20,
					WEIGHT_MIN = 0.0, WEIGHT_MAX = 1.0;
	if (package == 1)
	{
		{if (weight < WEIGHT_MAX && weight > WEIGHT_MIN)
			postage = LETTER;
		}
		{if (weight > WEIGHT_MAX)
			postage = (LETTER + (weight * LETTERplus));
		}
	}
	else if (package == 2)
	{
		{if (weight < WEIGHT_MAX && weight > WEIGHT_MIN)
			postage = ENVELOPE;
		}
		{if (weight > WEIGHT_MAX)
			postage = (ENVELOPE + (weight * ENVELOPEplus));
		}
	}
	else if (package == 3)
	{
		{if (weight < WEIGHT_MAX && weight > WEIGHT_MIN)
			postage = PARCEL;
		}
		{if (weight > WEIGHT_MAX)
			postage = (PARCEL + (weight * PARCELplus));
		}
	}


	//This part of the program breaks the zipcode into single digits.
	//First the ones, 
	 
	ones = zip % 10;
	
	//then tenths
	tens = zip % 100; 
	tensA = (tens - ones) / 10;
	
	//Hundreths
	hund = zip % 1000;
	hundA = (hund - tens) / 100;
	
	//Thousandths
	thou = zip % 10000;
	thouA = (thou - hund) / 1000;
	
	//Ten K
	tenK = zip % 100000;
	tenKA = (tenK - thou) / 10000;	
	 
	//This part of the program calculates the check number. 
	check = (ones - tensA + hundA + thouA + tenKA);
	check %= 10;
	check = (10-check);
	
	//This part of the program modifies the zip code to display 
	//it in true barcode form. 
	
	
		
	//This part of the program displays the postage label.
	cout << "\n\n\n\n";
	cout << "Your package will cost $" << postage << " to ship.\n\n";
	cout << left << "***********************************Postage\n\n";
	cout << name << "\n";
	cout << street << "\n";
	cout << city << " " << state << " " << zip << "\n\n";
	cout << "|" << tenKA << " " << thouA << " " << hundA;
	cout << " " << tensA << " " << ones << " " << check << "|";
	
	

	return 0;
}


Line 3: postage is an uninitialized variable.

Line 49,52: if weight == WEIGHT_MAX, you won't calculate postage at all.

Line 58,61: ditto

Line 67,70: ditto

Thank you! I kinda figured that out after some trial and error. I'm glad to KNOW now though thanks to your input.
Topic archived. No new replies allowed.