how to skip over code

Hi guys! I'm having trouble with a part of my code. I don't want it to output the given pollutant emission and if it is within allowable levels, I just want it to say "This is not within the range of allowed numbers" if the input is not within the range of allowed numbers. Any comments would be helpful!

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
 #include <iostream>
#include <string>

const int FIRST_STAGE_MILEAGE = 50000;
const int CARBON_MONOXIDE = 1;
const int HYDROCARBON = 2;
const int NITROGEN_OXIDE = 3;
const int NONMETHANE_HYDROCARBON = 4;
const int Quit = 0;
const int MileageBoundary1 = 0;
const int MileageBoundary2 = 100000;

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();
//function prototypes to get allowable level and determine if within bounds
double getAllowableLevel(double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer);



int main(int argc, char* argv[])
{
	int pollutant;
	double gramsPerMile;
	int odometer;
	bool compliant;
	double allowableGramsPerMile{};

	do
	{
		outputMenu();
		cout << "Enter pollutant number: ";
		cin >> pollutant;
		cout << "Enter grams emitted per mile: ";
		cin >> gramsPerMile;
		cout << "Enter odometer reading: ";
		cin >> odometer;

		switch (pollutant)
		{
		case CARBON_MONOXIDE:
			allowableGramsPerMile = getAllowableLevel(CARBON_MONOXIDE_ALLOWED1, CARBON_MONOXIDE_ALLOWED2, odometer);
			break;
		case HYDROCARBON:
			allowableGramsPerMile = getAllowableLevel(HYDROCARBON_ALLOWED1, HYDROCARBON_ALLOWED2, odometer);
			break;
		case NITROGEN_OXIDE:
			allowableGramsPerMile = getAllowableLevel(NITROGEN_OXIDE_ALLOWED1, NITROGEN_OXIDE_ALLOWED2, odometer);
			break;
		case NONMETHANE_HYDROCARBON:
			allowableGramsPerMile = getAllowableLevel(NONMETHANE_HYDROCARBON_ALLOWED1, NONMETHANE_HYDROCARBON_ALLOWED2, odometer);
			break;
		}

		string compliance;

		compliant = gramsPerMile <= allowableGramsPerMile;
		if (compliant)
			compliance = "within";
		else
			compliance = "exceeds";

		cout << "Emissions " << compliance << " allowable level of " << allowableGramsPerMile << " grams/mile" << endl;
	} while (pollutant != 0);


	cin.ignore();
	cin.get();
}


void outputMenu()
{
	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;
}


//Given an odometer reading, returns the allowable level
double getAllowableLevel(double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer)
{
	if (odometer > MileageBoundary2 || odometer < MileageBoundary1)
	{
		cout << "I'm sorry, mileage " << odometer << " must be within " << MileageBoundary1 << " - " << MileageBoundary2 << " miles." << endl;

	}

	if (odometer <= FIRST_STAGE_MILEAGE)
	{
		return gramsPerMileAllowed1;
	}
	else
	{
		return gramsPerMileAllowed2;
	}
cin.get();
}
Do you mean something like:

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
#include <iostream>
#include <string>

const int FIRST_STAGE_MILEAGE = 50000;
const int CARBON_MONOXIDE = 1;
const int HYDROCARBON = 2;
const int NITROGEN_OXIDE = 3;
const int NONMETHANE_HYDROCARBON = 4;
const int Quit = 0;
const int MileageBoundary1 = 0;
const int MileageBoundary2 = 100000;

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();
double getAllowableLevel(double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer);

int main()
{
	int pollutant {};
	double gramsPerMile {};
	int odometer {};
	double allowableGramsPerMile {};

	do {
		outputMenu();

		cout << "Enter pollutant number: ";
		cin >> pollutant;

		if (pollutant != Quit) {
			cout << "Enter grams emitted per mile: ";
			cin >> gramsPerMile;

			cout << "Enter odometer reading: ";
			cin >> odometer;

			switch (pollutant) {
				case CARBON_MONOXIDE:
					allowableGramsPerMile = getAllowableLevel(CARBON_MONOXIDE_ALLOWED1, CARBON_MONOXIDE_ALLOWED2, odometer);
					break;

				case HYDROCARBON:
					allowableGramsPerMile = getAllowableLevel(HYDROCARBON_ALLOWED1, HYDROCARBON_ALLOWED2, odometer);
					break;

				case NITROGEN_OXIDE:
					allowableGramsPerMile = getAllowableLevel(NITROGEN_OXIDE_ALLOWED1, NITROGEN_OXIDE_ALLOWED2, odometer);
					break;

				case NONMETHANE_HYDROCARBON:
					allowableGramsPerMile = getAllowableLevel(NONMETHANE_HYDROCARBON_ALLOWED1, NONMETHANE_HYDROCARBON_ALLOWED2, odometer);
					break;

				case Quit:
					break;

				default:
					cout << "Unknown pollutant\n";
			}

			if (gramsPerMile > allowableGramsPerMile)
				cout << "This is not within the range of allowed numbers\n";
		}
	} while (pollutant != Quit);
}


void outputMenu()
{
	cout << "\n(" << CARBON_MONOXIDE << ") Carbon monoxide\n";
	cout << "(" << HYDROCARBON << ") Hydrocarbons\n";
	cout << "(" << NITROGEN_OXIDE << ") Nitrogen oxides\n";
	cout << "(" << NONMETHANE_HYDROCARBON << ") Non-methane hydrocarbons\n";
	cout << "(" << Quit << ") Quit\n";
}

//Given an odometer reading, returns the allowable level
double getAllowableLevel(double gramsPerMileAllowed1, double gramsPerMileAllowed2, int odometer)
{
	if (odometer > MileageBoundary2 || odometer < MileageBoundary1)
		cout << "I'm sorry, mileage " << odometer << " must be within " << MileageBoundary1 << " - " << MileageBoundary2 << " miles." << endl;

	if (odometer <= FIRST_STAGE_MILEAGE)
		return gramsPerMileAllowed1;

	return gramsPerMileAllowed2;
}

Last edited on
Topic archived. No new replies allowed.