Need help inputting values with switch statements.

Hello, I'm currently writing a program that determines the cost of a travel based on travel distance, mpg, gas type, and either state (Michigan or Ohio). I am given values of the price of each gas type (regular, midgrade, and premium) for each state.

Michigan -
regular: 2.27
midgrade: 2.49
premium: 2.70

Ohio -
regular: 2.28
midgrade: 2.46
premium: 2.55

I've done everything so far and it works when I run it, besides the ending "total travel cost." I can't figure out how to input the prices of gas for whatever choice the user makes. Does this make sense? Thanks!

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
 #include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	int state;
	int gastype;
	int price; //price of gas
	int mpg;
	int distance;
	int cost; //final cost

	cout << "Welcome to the Travel Cost Calculator!" << endl;
	cout << " " << endl; //blank line
	cout << "Please select the state:" << endl;
	cout << "0 - Michigan" << endl;
	cout << "1 - Ohio" << endl;
	cout << "Choice: ";
	cin >> state;

	switch (state)
	{
	case 0:
		cout << "Michigan Selected" << endl;
		break;
	case 1:
		cout << "Ohio Selected" << endl;
		break;
	default:
		cout << "The number entered isn't valid." << endl;
	}

	//===============================================================================

	cout << " " << endl; //blank line
	cout << "Please select the gas type:" << endl;
	cout << "0 - Regular" << endl;
	cout << "1 - Midgrade" << endl;
	cout << "2 - Premium" << endl;
	cout << "Choice: ";
	cin >> gastype;

	switch (gastype)
	{
	case 0:
		cout << "Regular Selected" << endl;
		break;
	case 1:
		cout << "Midgrade Selected" << endl;
		break;
	case 2:
		cout << "Premium Selected" << endl;
		break;
	default:
		cout << "The number entered isn't valid." << endl;
	}

	//===============================================================================
	cout << " " << endl; //blank line
	cout << "Input MPG: ";
	cin >> mpg;
	//===============================================================================
	cout << " " << endl; //blank line
	cout << "Input Travel Distance (in miles): ";
	cin >> distance;
	//===============================================================================
	cost = distance / mpg * price;
	//===============================================================================
	cout << " " << endl; //blank line
	cout << "Your travel cost is: " << cost;
	//===============================================================================

	return 0;
}
Put your price calculation after the print statement in the case statement. For example:

1
2
3
4
5
6
7
8
9
//gastype
case 0:
    if(state == 0){ //Michigan Regular
        price = 2.27;
    } else if(state == 1){ //Ohio Regular
        price = 2.28;
    }
    break;
//... 


As a side note, I recommend googling "magic constants" or "magic numbers" in computer programming. It refers to your use of seemingly arbitrary numbers to represent things such as states or gas types.
Last edited on
I'll have to look that up! I took your advice and got this. However, the output doesn't turn out like I want it to. It is saying that "the variable 'cost' is being used without being initialized." And then it's not giving the correct output. Anyone spot an error?

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
#include <conio.h> // For function getch()
#include <cstdlib>  // For several general-purpose functions
#include <fstream>  // For file handling
#include <iomanip>;  // For formatted output
#include <iostream>  // For cin, cout, and system
#include <string>  // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	int state;
	int gastype;
	int price; //price of gas
	int mpg;
	int distance;
	int cost; //final cost

	cout << "Welcome to the Travel Cost Calculator!" << endl;
	cout << " " << endl; //blank line
	cout << "Please select the state:" << endl;
	cout << "0 - Michigan" << endl;
	cout << "1 - Ohio" << endl;
	cout << "Choice: ";
	cin >> state;

	switch (state)
	{
	case 0:
		cout << "Michigan Selected" << endl;
		break;
	case 1:
		cout << "Ohio Selected" << endl;
		break;
	default:
		cout << "The number entered isn't valid." << endl;
	}

	//===============================================================================

	cout << " " << endl; //blank line
	cout << "Please select the gas type:" << endl;
	cout << "0 - Regular" << endl;
	cout << "1 - Midgrade" << endl;
	cout << "2 - Premium" << endl;
	cout << "Choice: ";
	cin >> gastype;

	switch (gastype)
	{
	case 0:
		cout << "Regular Selected" << endl;
		if (state == 0) {
			price == 2.27;
		}
		else if (state == 1) {
			price = 2.28;
		}
		break;
	case 1:
		cout << "Midgrade Selected" << endl;
		if (state == 0) {
			price == 2.49;
		}
		else if (state == 1) {
			price = 2.46;
		}
		break;
	case 2:
		cout << "Premium Selected" << endl;
		if (state == 0) {
			price == 2.70;
		}
		else if (state == 1) {
			price = 2.55;
		}
		break;
	default:
		cout << "The number entered isn't valid." << endl;
	}

	//===============================================================================
	cout << " " << endl; //blank line
	cout << "Input MPG: ";
	cin >> mpg;
	//===============================================================================
	cout << " " << endl; //blank line
	cout << "Input Travel Distance (in miles): ";
	cin >> distance;
	//===============================================================================
	cost = distance / mpg * price;
	//===============================================================================
	cout << " " << endl; //blank line
	cout << "Your travel cost is: " << cost << endl;
	//===============================================================================

	return 0;
}
Simply Initialize your variables. Your main problem however is with "price" and not "cost".

1
2
3
if (state == 0) {
	price == 2.27;
}


== is an equal to operator. It checks if something is equal to something else.
= is an assignment operator. You want to assign a number to "price".

Also, price is of type "int". Meaning, if you assign it the value 2.27, or 2.9 etc, it will be rounded down, always, so your price would be 2 everywhere. You want to use "float" type instead, so you can use decimals.

float price = 0.0;

Same goes for cost. Change it to float instead of int. And just initialize it like float cost = 0.0;
That makes a lot of sense the way you explained it! Thanks so much!
Topic archived. No new replies allowed.