Switch and Nested if/else Statements

I have to write a program with a menu displayed like this:

(1) Carbon monoxide
(2) Hydrocarbons
(3) Nitrogen oxides
(4) Non-methane hydrocarbons

Enter pollutant number=> 3
Enter number of grams emitted per mile=> 0.42
Enter odometer reading=> 43268
Emissions exceed permitted level of 0.4 grams/mile.

The program asks the user to input the number of the pollutant they want to choose. Then it asks the user for the number of grams emitted per mile and the odometer reading. The program either tells the user if the grams/mile exceeds the permitted level or does not exceed the permitted level. I have to check for correct user input for the number corresponding to the pollutant, which I assume means confirm the selection with the user (could it mean something else?). I was told that I could check for correct user input with either a single if or a continuous loop. The main body of the program has to use a switch structure along with nested if/else statements. I can assume the the number of miles entered by the user will not surpass 100,000.

This is the table I'm using for the permitted emission levels:
1
2
3
4
5
				First 50,000 miles	Second 50,000 miles
Carbon monoxide			3.4 grams/mile		4.2 grams/mile
Hydrocarbons			0.31 grams/mile	        0.39 grams/mile
Nitrogen oxides			0.4 grams/mile		0.5 grams/mile
Non-methane hydrocarbons	0.25 grams/mile	        0.31 grams/mile


This table will not be displayed. I just used code function so the formatting of the table would show up properly.

I will post the code in the second post.


The things I'm not sure about are how to get the output statements so they look like they do above, how to get the user input and then display it to the user, and how to make the if/else switch body of the program.

I initially had many more variables. I created variables for the permitted level of each pollutant in the <50,000 miles category and the >50,000 miles category. I realized that I the switch and if/else statements could handle that though and I was not told that the permitted level values would be changed later on.

I used type casting in the switch. The requirement is to use a switch but I wasn’t sure how to get the values for the control expression and the cases to be integer values. Based on the way I did it,

I put things like case 341 and case 1 because I assume any value entered will be default to the lowest value. I’m pretty sure this is wrong. I would like to find out how to use switches properly. Maybe a char should be used? From the class I’m currently enrolled in and reading the site I get the basic idea and really simple examples but I’ve never seen more complex examples. I guess the only way to really learn and to read other people’s code?

I basically wrote the code as cout statements, an if statement, and 3 else if statements.
Last edited on
I would also like to know how to \n instead of endl;. Would it look like this cout << “Statement\n”?

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>		// Preprocessor directives
using namespace std;

int main ()			// The program starts here
{
	// Declaring variables
	
	int pollutant_number(0);
	int odometer_reading(0);
	double grams_emitted_per_mile(0);

	//Information displayed to the user
	cout << "Input the number of the pollutant you want to choose." << endl;
	
	cout << "\n" << endl;
	
	cout << "(1) Carbon monoxide" << endl;
	cout << "(2) Hydrocarbons" << endl;
	cout << "(3) Nitrogen oxides" << endl;
	cout << "(4) Non-methane hydrocarbons" << endl;
	
	cout << "\n" << endl;

	cout << "Enter pollutant number=> " << pollutant_number << endl;
	cin >> pollutant_number;
	
	cout << "Enter number of grams emitted per mile=> " << grams_emitted_per_mile << endl;
	cin >> grams_emitted_per_mile;

	cout << "Enter odometer reading=> " << odometer_reading << endl;
	cin >> odometer_reading;

	cout << "Emissions";		// This line needs to be finished

	
	if (pollutant_number == 1)		// Supossed to confirm that user choose the correct pollutant
	{
		cout << "You have selected Carbon monoxide." << endl;
	}
		if (odometer_reading <= 50,000)		// Seperates odometer readiing values
		{
			switch ((int) grams_emitted_per_mile*100)
			{
			case 341:
				cout << "Emissions exceed permitted level of 3.4 grams/mile" << endl;
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 3.4 grams/mile" << endl;
				break;
			}
		}
		else if (odometer_reading > 50,000)		// Seperates odometer readiing values
		{
				switch ((int) grams_emitted_per_mile*100)
				{
				case 421:
					cout << "Emissions exceed permitted level of 4.2 grams/mile" << endl;
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 4.2 grams/mile" << endl;
					break;
				}
		}

		
	else if (pollutant_number == 2)		// Supossed to confirm that user choose the correct pollutant
	{
		cout << "You have selected Hydrocarbons." << endl;
	}
		if (odometer_reading <= 50,000)		// Seperates odometer readiing values
		{
			switch ((int) grams_emitted_per_mile*100)
			{
			case 32:
				cout << "Emissions exceed permitted level of 0.31 grams/mile" << endl;
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.31 grams/mile" << endl;
				break;
			}
		}
		else if (odometer_reading > 50,000)		// Seperates odometer readiing values
		{
				switch ((int) grams_emitted_per_mile*100)
				{
				case 40:
					cout << "Emissions exceed permitted level of 0.39 grams/mile" << endl;
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 0.39 grams/mile" << endl;
					break;
				}
		}
	

	else if (pollutant_number == 3)		// Supossed to confirm that user choose the correct pollutant
	{
		cout << "You have selected Nitrogen oxides." << endl;
	}
		if (odometer_reading <= 50,000)		// Seperates odometer readiing values
		{
			switch ((int) grams_emitted_per_mile*100)
			{
			case 41:
				cout << "Emissions exceed permitted level of 0.4 grams/mile" << endl;
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.4 grams/mile" << endl;
				break;
			}
		}
		else if (odometer_reading > 50,000)		// Seperates odometer readiing values
		{
				switch ((int) grams_emitted_per_mile*100)
				{
				case 51:
					cout << "Emissions exceed permitted level of 0.5 grams/mile" << endl;
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 0.5 grams/mile" << endl;
					break;
				}
		}
	
	
	else if (pollutant_number == 4)		// Supossed to confirm that user choose the correct pollutant
	{
		cout << "You have selected Carbon monoxide." << endl;
	}
		if (odometer_reading <= 50,000)		// Seperates odometer readiing values
		{
			switch ((int) grams_emitted_per_mile*100)
			{
			case 26:
				cout << "Emissions exceed permitted level of 0.25 grams/mile" << endl;
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.25 grams/mile" << endl;
				break;
			}
		}
		else if (odometer_reading > 50,000)		// Seperates odometer readiing values
		{
				switch ((int) grams_emitted_per_mile*100)
				{
				case 32:
					cout << "Emissions exceed permitted level of 0.31 grams/mile" << endl;
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 0.31 grams/mile" << endl;
					break;
				}
		}

return 0;	// Ends Program
}
First, yes, just include \n in a string for a newline character:

 
cout << "Print this then a newline\n";


Your code:
 
cout << "Enter number of grams emitted per mile=> " << grams_emitted_per_mile << endl;


Not sure why, but you're outputting the value while asking for the input and, usually, those values are zero anyway. I think a simpler way would be this:
1
2
cout << "Enter number of grams emitted per mile=>";
cin >> grams_emitted_per_mile;


The code's not bad, but needs some tweak. There's gaps.

For instance, you probably want to validate the polluntant_type entered. What happens if, say, I enter 5?

Also, what if a value doesn't match something in your switch? It's going to leak through.

Here's a basic switch example:

Switch statement
1
2
3
4
5
6
7
8
9
10
11
12
int my_value = 2;

switch (my_value)  // Check the value of my_value
case 1:                   // If the value is 1, this block of code runs
   cout << "my_value is 1\n";
   break;                 // We use the break to break out of the switch.  Otherwise, it would
                              // continue evaluating any cases below this one.
case 2:                  // If the value is 2, this block of code runs
   cout << "my_value is 2\n";
   break;
default:                 // This runs if non of the cases are matched
   cout << "my_value is not 1 or 2"\n; 


The default value is pretty essentially. Syntactically you can omit it and your code would compile. Realistically, you always want it there, to know how to handle something that doesn't match any of the cases. The breaks are needed to break out of the switch but, again, aren't essentially. This is purely requirements based (you may want other cases to be evaluated or have multiple cases do the same thing. This is known as a fall-through.

Fall through example
1
2
3
4
5
6
7
8
9
int my_value = 2;

switch (my_value)
case 1:
case 2:
  cout << "my_value is 1 or 2\n";
  break;
default:
  cout << "my_value is not 1 or 2\n";


Be careful if you're casting from double to ints. Remember, C++ compilers will truncate the value, not round up or down. Meaning that:

1
2
double my_double = 6.9;
double my_int = (int)my_double;  // This value will be 6 


Hopefully that nudges you in the right direction. Any more questions, shout up.
Just a notice to your code. You don't need to use an 'else if' because it's always true. Just use an else.

else if (odometer_reading > 50,000)
replace with just an else.
I changed the code around because in the assignment the question was “What will be the condition you check in the outer level of the if/else structure (will be the same for each pollutant case)?” and the next question was “What will be the condition you check in the inner level of the if/else structure (will NOT be the same for each pollutant case)?” so I made the outer if/else if check the odometer reading, the inner if check the pollutant type, and made a switch within the inner if/else if. Would that be the best method based on the constraints of the assignment? It seems like a lot of code for what the program is doing.

Not sure why, but you're outputting the value while asking for the input and, usually, those values are zero anyway. I think a simpler way would be this:
1
2
cout << "Enter number of grams emitted per mile=>";
cin >> grams_emitted_per_mile;


I want the value the user enters to be formatted like how I have in the first post. I'm not sure how do that. I want to get the user input but I also want it displayed on the same like as the cout statement. I also want that to happen with the pollutant number and odometer reading cout statements.

For instance, you probably want to validate the polluntant_type entered. What happens if, say, I enter 5?


I'm not sure how to do that. This is what the assignment said "You should check for correct user input for the number corresponding to the pollutant – you may do this either with a single if, or with a continuous loop, if you desire." I don't know how I would check for correct input. What would I compare it to? The only thing I could think of was informing the user of what he or she selected but that doesn't seem right.

Also, what if a value doesn't match something in your switch? It's going to leak through.


I'm not sure how to make a switch. Every example I see looks like case 1, case 2, answer 1, answer 2, etc...The assignment said toy use a switch so I multiplied the numbers in the table by 100 so they would become integers. Then I made each case the bound (so if permitted level is 340 I named the case 341). That seems very wrong.

In class today we learned about do and do while loops. Using using a loop be better than the if/else if and the switch in this program? I also was dude I could put my code inside of a do while statement so the user can run the program as many times as he or she wants.

When I compiled my cout statements had 0 where 3 and 43268 are in the first post. Also the program just says says invalid input. I assume that from the badly designed switch.


Just a notice to your code. You don't need to use an 'else if' because it's always true. Just use an else.


I made the value from 50,000 to 100,000 instead. Would a else if be used there then? By the way, is it called an if else or an else if?
Here is 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*	This program compares the user entered value of grams of pollutant emitted per mile and informs
	the user if the emissions are above or a below the permitted level.											        */
//	The user will input the number of grams of pollutant emitted per mile along with his or her odometer reading.
/*	The program compares the user entered value of grams emitted per mile and compares it to values for the
	permitted level of emmissions for the selected pollutant.															*/
/*	The program will output a menu for the user to select one of four pollutants. It will also display the 
	user's choice, grams emitted per mile, odometer reading, and if the calculated grams/mile is above or below 
	a permitted emissions level.																				        */
//**************************************************************************************************************************

#include <iostream>		// Preprocessor directives
using namespace std;

int main ()			// The program starts here
{
	// Declaring variables
	
	int pollutant_number(0);
	int odometer_reading(0);
	double grams_emitted_per_mile(0);
	char answer;

	do		// Allow for multiple runs of the program with a do-while loop
	{
	//Information displayed to the user
	cout << "Input the number of the pollutant you want to choose." << endl;
	
	cout << "\n" << endl;
	
	cout << "(1) Carbon monoxide" << endl;
	cout << "(2) Hydrocarbons" << endl;
	cout << "(3) Nitrogen oxides" << endl;
	cout << "(4) Non-methane hydrocarbons" << endl;
	
	cout << "\n" << endl;

	cout << "Enter pollutant number=> " << pollutant_number << endl;
	cin >> pollutant_number;
	
	cout << "Enter number of grams emitted per mile=> \n";
	cin >> grams_emitted_per_mile;

	cout << "Enter odometer reading=> " << odometer_reading << endl;
	cin >> odometer_reading;

	cout << "Emissions\n\n";		// This line needs to be finished

	
	if (odometer_reading <= 50,000)		// Odometer reading <50,000 miles
	{	
		if (pollutant_number == 1) 
		{
			cout << "You have selected Carbon monoxide.\n";
			switch ((int) grams_emitted_per_mile*100)
			{
			case 341:
				cout << "Emissions exceed permitted level of 3.4 grams/mile\n";
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 3.4 grams/mile\n";
				break;
			default:
				cout << "Invalid input\n";
			}
		}
		else if (pollutant_number == 2) 
		{
			cout << "You have selected Hydrocarbons.\n";
			switch ((int) grams_emitted_per_mile*100)
			{
			case 32:
				cout << "Emissions exceed permitted level of 0.31 grams/mile\n";
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.31 grams/mile\n"; 
				break;
			default:
				cout << "Invalid input\n";
			}
		}
		else if (pollutant_number == 3) 
		{
			cout << "You have selected Nitrogen oxides.\n";
			switch ((int) grams_emitted_per_mile*100)
			{
			case 41:
				cout << "Emissions exceed permitted level of 0.4 grams/mile\n";
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.4 grams/mile\n";
				break;
			default:
				cout << "Invalid input\n";
			}
		}
		else if (pollutant_number == 4)
		{
			cout << "You have selected Carbon monoxide.\n";
			switch ((int) grams_emitted_per_mile*100)
			{
			case 26:
				cout << "Emissions exceed permitted level of 0.25 grams/mile\n";
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.25 grams/mile\n";
				break;
			default:
				cout << "Invalid input\n";
			}
		}
	}
		

	if (odometer_reading > 50,000 && odometer_reading <= 100,000)	// Odometer between 50,000 and 100,000 miles
	{
		if (pollutant_number == 1)
		{
			cout << "You have selected Carbon monoxide.\n";
			switch ((int) grams_emitted_per_mile*100)
				{
				case 421:
					cout << "Emissions exceed permitted level of 4.2 grams/mile\n";
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 4.2 grams/mile\n";
					break;
				default:
				cout << "Invalid input\n";
				}
		}
		else if (pollutant_number == 2)
		{
			cout << "You have selected Hydrocarbons.\n";
			switch ((int) grams_emitted_per_mile*100)
				{
				case 40:
					cout << "Emissions exceed permitted level of 0.39 grams/mile\n";
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 0.39 grams/mile\n";
					break;
				default:
				cout << "Invalid input\n";
				}
		}
		else if (pollutant_number == 3)
		{
			cout << "You have selected Nitrogen oxides.\n";
			switch ((int) grams_emitted_per_mile*100)
				{
				case 51:
					cout << "Emissions exceed permitted level of 0.5 grams/mile\n";
					break;
				case 1:
					cout << "Emissions do not exceed permitted level of 0.5 grams/mile\n";
					break;
				default:
				cout << "Invalid input\n";
				}
		}
		else if (pollutant_number == 4)
		{
			cout << "You have selected Carbon monoxide.\n";
			switch ((int) grams_emitted_per_mile*100)
			{
			case 32:
				cout << "Emissions exceed permitted level of 0.31 grams/mile\n";
				break;
			case 1:
				cout << "Emissions do not exceed permitted level of 0.31 grams/mile\n";
				break;
			default:
				cout << "Invalid input\n";
			}
		}
	}
	cout << "Would you like to run the program again?\n";	// Allows program to be run multiple times
		cin >> answer;
	}
	while (answer == 'y' || answer == 'Y');	

return 0;
}
This assignment is due tonight at 11PM and I have a lot of other homework. Does anybody know how to fix the switch statements and display to the user if his or her value exceeds the permitted level? Also is it good or bad coding to put a program inside of a do while loop?
You should be using if statements in this case, as you want to display messages for all input > 32, or whatever the case may be.

There's nothing wrong with putting the code in a do while loop if you want to run it again.
You need to swap your switches and ifs. Checking for pollutant type should be a switch, and checking to see if the emissions exceed the permitted level should be an if/else statement.
Here is my 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>		// Preprocessor directives
using namespace std;

int main ()			// The program starts here
{
	
	// Declaring variables
	int pollutant_number(0);
	int odometer_reading(0);
	double grams_emitted_per_mile(0);
	char answer;

	do		// Allow for multiple runs of the program with a do-while loop
	{
	
	//Information displayed to the user
	cout << "Input the number of the pollutant you want to choose.\n";
	
	cout << "\n";
	
	cout << "(1) Carbon monoxide\n";
	cout << "(2) Hydrocarbons\n";
	cout << "(3) Nitrogen oxides\n";
	cout << "(4) Non-methane hydrocarbons\n";
	
	cout << "\n";

	cout << "Enter pollutant number=> ";
	cin >> pollutant_number;
	
	cout << "Enter number of grams emitted per mile=> ";
	cin >> grams_emitted_per_mile;

	cout << "Enter odometer reading=> ";
	cin >> odometer_reading;

	cout << "Emissions\n\n";		// This line needs to be finished

	if (pollutant_number > 4)
	{
		cout << "Invalid selection";
	}

	
	if (odometer_reading <= 50,000)		// Odometer reading <50,000 miles
	{	
		switch (pollutant_number)	// Expression to match cases to  
		{
		case 1:
			cout << "You have selected Carbon monoxide.\n";
			if (grams_emitted_per_mile > 3.4)
			{
				cout << "Emissions exceed permitted level of 3.4 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 3.4 grams/mile\n";
			}
			break;
		case 2:
			cout << "You have selected Hydrocarbons.\n";
			if (grams_emitted_per_mile > 0.31)
			{
				cout << "Emissions exceed permitted level of 0.31 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 0.31 grams/mile\n";
			}
			break;
		case 3:
			cout << "You have selected Nitrogen oxides.\n";
			if (grams_emitted_per_mile > 0.4)
			{
				cout << "Emissions exceed permitted level of 0.4 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 0.4 grams/mile\n";
			}
			break;
		case 4:
			cout << "You have selected Carbon monoxide.\n";
			if (grams_emitted_per_mile > 0.25)
			{
				cout << "Emissions exceed permitted level of 0.25 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 0.25 grams/mile\n";
			}
			break;
		default:
			cout << "Invalid selection\n";
		}
	}

	else if (odometer_reading > 50,000 && odometer_reading <= 100,000)	// Odometer between 50,000 and 100,000 miles
	{
		switch (pollutant_number)	// Expression to match cases to  
		{
		case 1:
			cout << "You have selected Carbon monoxide.\n";
			if (grams_emitted_per_mile > 4.2)
			{
				cout << "Emissions exceed permitted level of 4.2 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 4.2 grams/mile\n";
			}
			break;
		case 2:
			cout << "You have selected Hydrocarbons.\n";
			if (grams_emitted_per_mile > 0.39)
			{
				cout << "Emissions exceed permitted level of 0.39 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 0.39 grams/mile\n";
			}
			break;
		case 3:
			cout << "You have selected Nitrogen oxides.\n";
			if (grams_emitted_per_mile > 0.5)
			{
				cout << "Emissions exceed permitted level of 0.5 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 0.5 grams/mile\n";
			}
			break;
		case 4:
			cout << "You have selected Carbon monoxide.\n";
			if (grams_emitted_per_mile > 0.31)
			{
				cout << "Emissions exceed permitted level of 0.31 grams/mile\n";
			}
			else
			{
				cout << "Emissions do not exceed permitted level of 0.31 grams/mile\n";
			}
			break;
			default:
			cout << "Invalid selection\n";
		}
	}
	else
	{
		cout << "Invalid selection";
	}
	
	cout << "Would you like to run the program again?\n";	// Allows program to be run multiple times
		cin >> answer;
	}
	while (answer == 'y' || answer == 'Y');	

return 0;
}


I still have several problems. I want the formatting of the cout statements to be like how I have it in the original post. I don't know if there is some way to "link" the cout statements from the if statements to the 4th "Emissions exceed permitted level" line.

I added in if statement at the beginning of the program to prevent users from inputing an invalid selection but that appear to be working. The same goes for the other user input (grams/mile and odometer reading). Is there is a simple way to make an error guard so the user can only input value I want them too? Maybe using an if statement?
Sorry about posting the long code several times. Would liking to a site like ideone be better? Hopefully people have a monitor in portrait mode.

My only real problem now is getting the program to tell the user if their pollutant level is above or below the permitted level.
Last edited on
I don't remember off the top of my head about value checking, but for formatting cout statements, look up the iomanip library.
I've only used iomanip for displaying significant figures or decimal. The assignment is due at 11 so I don't have much time to learn and implement things. It seems like I'm pretty close just the cout statements in the if statements won't print.
Topic archived. No new replies allowed.