switch not working properly

My switch is not working properly with a program. The program is ignoring every "case" but not the default. Do you know what's the problem??


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
// switch.cpp : main project file.


#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int type;

int main()
{

	cout << "Type a number from 1 to 3:;
	cin >> type;

	switch ( type ){

		case '1':
			
				cout << "1";
			break;
		case '2':
			
				cout << "2";
			
			break;

		case '3':
				cout << "3";
			break;

		case '\n':
		case '\t':
		case ' ':
			break;

		default:
			cout << "You typed an invalid number." << endl;
			break;



	}

	

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

	return 0;
} 


There is 2 ways to fix this.

1) read in a char

int type; -> char type;

2) or change your cases to integers.

case '1': -> case 1:
case '2': -> case 2:
case '3': -> case 3:
Last edited on
Thanks, it worked. :)
Topic archived. No new replies allowed.