Please I need help with this. I was already given a starting program, now I just
ID: 3851859 • Letter: P
Question
Please I need help with this.
I was already given a starting program, now I just need to complete it. I am writing in C++ language.
Here is the given program:
#include <iostream>
#include <string>
const int CARBON_MONOXIDE = 1;
const int HYDROCARBON = 2;
const int NITROGEN_OXIDE = 3;
const int NONMETHANE_HYDROCARBON = 4;
const int QUIT = 0;
const double CARBON_MONOXIDE_ALLOWED1 = 3.4;
const double CARBON_MONOXIDE_ALLOWED2 = 4.2;
const double HYDROCARBON_ALLOWED1 = 0.31;
const double HYDROCARBON_ALLOWED2 = 0.39;
const double NITROGEN_OXIDE_ALLOWED1 = 0.4;
const double NITROGEN_OXIDE_ALLOWED2 = 0.5;
const double NONMETHANE_HYDROCARBON_ALLOWED1 = 0.25;
const double NONMETHANE_HYDROCARBON_ALLOWED2 = 0.31;
using namespace std;
void outputMenu();
int main(int argc, char *argv[])
{
int pollutant;
int odometer;
double gramsPerMile;
outputMenu();
cin >> pollutant;
while (pollutant != QUIT)
{
cout << endl;
cout << "Enter grams emitted per mile: ";
cin >> gramsPerMile;
cout << "Enter odometer reading: ";
cin >> odometer;
switch (pollutant)
{
case CARBON_MONOXIDE:
break;
case HYDROCARBON:
break;
case NITROGEN_OXIDE:
break;
case NONMETHANE_HYDROCARBON:
break;
}
outputMenu();
cin >> pollutant;
}
cin.ignore();
cin.get();
}
void outputMenu()
{
cout << endl;
cout << "(" << CARBON_MONOXIDE << ") Carbon monoxide" << endl;
cout << "(" << HYDROCARBON << ") Hydrocarbons" << endl;
cout << "(" << NITROGEN_OXIDE << ") Nitrogen oxides" << endl;
cout << "(" << NONMETHANE_HYDROCARBON << ") Non-methane hydrocarbons" << endl;
cout << "(" << QUIT << ") Quit" << endl;
cout << "Enter choice: ";
}
These are what the professor wants us to do:
1. Not change any of the given code.
2. Use the defined constant variables for any constant values in your program.
3. Create additional constant variables to use when checking the mileage boundaries (50000 miles and 100000 miles).
4. Output an indication that an invalid odometer reading has been entered (outside of the range of 0-100000).
5. Use the switch statement started in the program for your decision structure.
6. Have at least one other user defined function. One possibility is the following function that determines the allowable level for a particular pollutant:
double getAllowableLevel (double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer);
Thank you so much in advance.
Explanation / Answer
// C++ code
#include <iostream>
#include <string>
const int CARBON_MONOXIDE = 1;
const int HYDROCARBON = 2;
const int NITROGEN_OXIDE = 3;
const int NONMETHANE_HYDROCARBON = 4;
const int QUIT = 0;
const double CARBON_MONOXIDE_ALLOWED1 = 3.4;
const double CARBON_MONOXIDE_ALLOWED2 = 4.2;
const double HYDROCARBON_ALLOWED1 = 0.31;
const double HYDROCARBON_ALLOWED2 = 0.39;
const double NITROGEN_OXIDE_ALLOWED1 = 0.4;
const double NITROGEN_OXIDE_ALLOWED2 = 0.5;
const double NONMETHANE_HYDROCARBON_ALLOWED1 = 0.25;
const double NONMETHANE_HYDROCARBON_ALLOWED2 = 0.31;
const int LIMIT1 = 50000;
const int LIMIT2 = 100000;
using namespace std;
void outputMenu();
double getAllowableLevel (double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer);
int main(int argc, char *argv[])
{
int pollutant;
int odometer;
double gramsPerMile;
outputMenu();
cin >> pollutant;
while (pollutant != QUIT)
{
cout << endl;
cout << "Enter grams emitted per mile: ";
cin >> gramsPerMile;
cout << "Enter odometer reading: ";
cin >> odometer;
if(odometer > 100000 || odometer < 0)
{
cout << "Mileage " << odometer << " must be within 0-100000 miles ";
continue;
}
switch (pollutant)
{
case CARBON_MONOXIDE:
cout << " Emissions within allowable level of " << getAllowableLevel(CARBON_MONOXIDE_ALLOWED1, CARBON_MONOXIDE_ALLOWED2, odometer) << " grams/mile " << endl << endl;
break;
case HYDROCARBON:
cout << " Emissions within allowable level of " << getAllowableLevel(HYDROCARBON_ALLOWED1, HYDROCARBON_ALLOWED2, odometer) << " grams/mile " << endl << endl;
break;
case NITROGEN_OXIDE:
cout << " Emissions within allowable level of " << getAllowableLevel(NITROGEN_OXIDE_ALLOWED1, NITROGEN_OXIDE_ALLOWED2, odometer) << " grams/mile " << endl << endl;
break;
case NONMETHANE_HYDROCARBON:
cout << " Emissions within allowable level of " << getAllowableLevel(NONMETHANE_HYDROCARBON_ALLOWED1, NONMETHANE_HYDROCARBON_ALLOWED2, odometer) << " grams/mile " << endl << endl;
break;
}
outputMenu();
cin >> pollutant;
}
// cin.ignore();
// cin.get();
return 0;
}
void outputMenu()
{
cout << endl;
cout << "(" << CARBON_MONOXIDE << ") Carbon monoxide" << endl;
cout << "(" << HYDROCARBON << ") Hydrocarbons" << endl;
cout << "(" << NITROGEN_OXIDE << ") Nitrogen oxides" << endl;
cout << "(" << NONMETHANE_HYDROCARBON << ") Non-methane hydrocarbons" << endl;
cout << "(" << QUIT << ") Quit" << endl;
cout << "Enter choice: ";
}
double getAllowableLevel (double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer)
{
if(odometer <= LIMIT1)
{
return gramsPerMileAllowed1;
}
else if(odometer <= LIMIT2)
{
return gramsPerMileAllowed2;
}
}
/*
output:
(1) Carbon monoxide
(2) Hydrocarbons
(3) Nitrogen oxides
(4) Non-methane hydrocarbons
(0) Quit
Enter choice: 1
Enter grams emitted per mile: 3.2
Enter odometer reading: 77000
Emissions within allowable level of 4.2 grams/mile
(1) Carbon monoxide
(2) Hydrocarbons
(3) Nitrogen oxides
(4) Non-methane hydrocarbons
(0) Quit
Enter choice: 2
Enter grams emitted per mile: 0.25
Enter odometer reading: 23456
Emissions within allowable level of 0.31 grams/mile
(1) Carbon monoxide
(2) Hydrocarbons
(3) Nitrogen oxides
(4) Non-methane hydrocarbons
(0) Quit
Enter choice: 3
Enter grams emitted per mile: 0.7
Enter odometer reading: 100100
Mileage 100100 must be within 0-100000 miles
Enter grams emitted per mile: 0.7
Enter odometer reading: 16478
Emissions within allowable level of 0.4 grams/mile
(1) Carbon monoxide
(2) Hydrocarbons
(3) Nitrogen oxides
(4) Non-methane hydrocarbons
(0) Quit
Enter choice: 0
*/