Switch inside If-Else statement error?

It won't output anything, i am pretty sure my error is in the switch statement when i use the keyword as (m). I don't think it is using the same m as the user input one, but I don't know why. Would it be easier/more robust to put the if-else's inside the switch?

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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
	double m;
	double d;
	double y;
	


	cout<<"Please input your date of birth in the format mm dd yy"<<endl;
	cin>>m>>d>>y;
	
	if (y>14)
	{

	
	switch(m)
	{case '01':
	cout<<"Your birthday is January "<<d<<", 19"<<y;
	break;
	case '02':
	cout<<"Your birthday is February "<<d<<", 19"<<y;
	break;
	case '03':
	cout<<"Your birthday is March "<<d<<", 19"<<y;
	break;
	case '04':
	cout<<"Your birthday is April "<<d<<", 19"<<y;
	break;
	case '05':
	cout<<"Your birthday is May "<<d<<", 19"<<y;
	break;
	case '06':
	cout<<"Your birthday is June "<<d<<", 19"<<y;
	break;
	case '07':
	cout<<"Your birthday is July "<<d<<", 19"<<y;
	break;
	case '08':
	cout<<"Your birthday is August "<<d<<", 19"<<y;
	break;
	case '09':
	cout<<"Your birthday is September "<<d<<", 19"<<y;
	break;
	case '10':
	cout<<"Your birthday is October "<<d<<", 19"<<y;
	break;
	case '11':
	cout<<"Your birthday is November "<<d<<", 19"<<y;
	break;
	case '12':
	cout<<"Your birthday is December "<<d<<", 19"<<y;
	break;

	}
	}
	else
	{
		switch(m)
	{case '01':
	cout<<"Your birthday is January "<<d<<", 20"<<y;
	break;
	case '02':
	cout<<"Your birthday is February "<<d<<", 20"<<y;
	break;
	case '03':
	cout<<"Your birthday is March "<<d<<", 20"<<y;
	break;
	case '04':
	cout<<"Your birthday is April "<<d<<", 20"<<y;
	break;
	case '05':
	cout<<"Your birthday is May "<<d<<", 20"<<y;
	break;
	case '06':
	cout<<"Your birthday is June "<<d<<", 20"<<y;
	break;
	case '07':
	cout<<"Your birthday is July "<<d<<", 20"<<y;
	break;
	case '08':
	cout<<"Your birthday is August "<<d<<", 20"<<y;
	break;
	case '09':
	cout<<"Your birthday is September "<<d<<", 20"<<y;
	break;
	case '10':
	cout<<"Your birthday is October "<<d<<", 20"<<y;
	break;
	case '11':
	cout<<"Your birthday is November "<<d<<", 20"<<y;
	break;
	case '12':
	cout<<"Your birthday is December "<<d<<", 20"<<y;
	break;

		}
	}
	system("Pause");
	return 0;
}
I thought my problem was having doubles instead of ints, but that wasn't so.
switch statements can only use integers, not floats/doubles. Your compiler is automatically converting the double to an int.
Last edited on
ok thank you, I'll go back and fix some a lot.
You could also put the month names in an array ( lets call it 'months' ), and then you can print that month with months[m] instead of writing it out for each possibility.
I am doing this for an assignment, I "have" to use a switch statement. Been reading for a while now, not sure how to use the switch when the user is supposed to input a two digit number.
case '12':

Also, these case statements are incorrect. '12' is not a character. If you want the number 12, just write 12:

case 12:
oh ok, would i only use the single quotes if it were a character?
Yes, and double quotes if it was a string. Since these are numbers, you don't use anything. ( btw a string wouldn't work in a switch statement )
Ok, thanks guys. Random, but in this case, or any other on c++, would 01 = 0? As in, does the compiler/code recognize them as the same. If the user input 01, would it recognize it as 1 if i put it as case 1? sorry, i'm new to coding (obviously).
01 would be saved as 1.
ok. here is my revised code so far, still same result though.
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
int main()
{
	int m;
	int d;
	int y;
	


	cout<<"Please input your date of birth in the format mm dd yy"<<endl;
	cin>>m>>d>>y;
	
	switch(m)
		

if (y>14)
	{

	
	switch(m)
	{case 1:
	cout<<"Your birthday is January "<<d<<", 19"<<y;
	break;
	case 2:
	cout<<"Your birthday is February "<<d<<", 19"<<y;
	break;
	case 3:
	cout<<"Your birthday is March "<<d<<", 19"<<y;
	break;
	case 4:
	cout<<"Your birthday is April "<<d<<", 19"<<y;
	break;
	case 5:
	cout<<"Your birthday is May "<<d<<", 19"<<y;
	break;
	case 6:
	cout<<"Your birthday is June "<<d<<", 19"<<y;
	break;
	case 7:
	cout<<"Your birthday is July "<<d<<", 19"<<y;
	break;
	case 8:
	cout<<"Your birthday is August "<<d<<", 19"<<y;
	break;
	case 9:
	cout<<"Your birthday is September "<<d<<", 19"<<y;
	break;
	case 10:
	cout<<"Your birthday is October "<<d<<", 19"<<y;
	break;
	case 11:
	cout<<"Your birthday is November "<<d<<", 19"<<y;
	break;
	case 12:
	cout<<"Your birthday is December "<<d<<", 19"<<y;
	break;

	}
	}
	else
	{
		switch(m)
	{case 1:
	cout<<"Your birthday is January "<<d<<", 20"<<y;
	break;
	case 2:
	cout<<"Your birthday is February "<<d<<", 20"<<y;
	break;
	case 3:
	cout<<"Your birthday is March "<<d<<", 20"<<y;
	break;
	case 4:
	cout<<"Your birthday is April "<<d<<", 20"<<y;
	break;
	case 5:
	cout<<"Your birthday is May "<<d<<", 20"<<y;
	break;
	case 6:
	cout<<"Your birthday is June "<<d<<", 20"<<y;
	break;
	case 7:
	cout<<"Your birthday is July "<<d<<", 20"<<y;
	break;
	case 8:
	cout<<"Your birthday is August "<<d<<", 20"<<y;
	break;
	case 9:
	cout<<"Your birthday is September "<<d<<", 20"<<y;
	break;
	case 10:
	cout<<"Your birthday is October "<<d<<", 20"<<y;
	break;
	case 11:
	cout<<"Your birthday is November "<<d<<", 20"<<y;
	break;
	case 12:
	cout<<"Your birthday is December "<<d<<", 20"<<y;
	break;

		}
	}

	system("Pause");
	return 0;
}
Line 12 shouldn't be there.
wow, such a silly mistake. thank you a lot, it compiles now. just need some formatting.
since you are using double (floating point number/integer) as condition on your control statements you dont have to put ' ' on it unless you are using char (character)
Topic archived. No new replies allowed.