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.