Entering a vehicle class gives an error.

Hello cplusplus forum,
can someone point me in the right direction so the vehicle class accepts the options, such as Bus.
When entering a vehicle class, the result is always, Unknown vehicle class!



#include <iostream>
using namespace std;

int main()
{
int vehicleClass;
double toll;
cout << "Enter vehicle class. E.g.: Passenger car. Bus. Truck.: ";
cin >> vehicleClass;

switch (vehicleClass)
{
case 1:
cout << "Passenger car.";
toll = 0.50;
break;
case 2:
cout << "Bus.";
toll = 1.50;
break;
case 3:
cout << "Truck.";
toll = 2.00;
break;
default:
cout << "Unknown vehicle class!";
}
}

// Running doesn't work when you enter a vehicle class.
Variable vehicleClass has type int because you defined it as

int vehicleClass;

So it can accept only integer numbers as 1, 2, -10 and so on. You can not enter a string into the variable.
Last edited on
so, you should give the users proper instructions, for example:
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
#include <iostream>
using namespace std;

int main()
{
	int vehicleClass;
	double toll;
	cout << "Enter vehicle class." << endl
	     << "1. Passenger car." << endl
	     << "2. Bus." << endl
	     << "3. Truck." << endl
	     << "choice: ";

	cin >> vehicleClass;

	switch (vehicleClass)
	{
		case 1:
			cout << "Passenger car.";
			toll = 0.50;
			break;
		case 2:
			cout << "Bus.";
			toll = 1.50;
			break;
		case 3:
			cout << "Truck.";
			toll = 2.00;
			break;
		default:
			cout << "Unknown vehicle class!";
	}
	cin.get();
	return 0;	
}


just an opinion, IMHO
Topic archived. No new replies allowed.