mm/dd/yy conversion switch case

so im taking my first programming class for scientist and engineers using c++ and have been doing OK untill this program.

the homework problem seemed very simple, convert mm/dd/yyyy to a March 31ST, 1985 format. i decided to use a switch case for months and days after i checked for leap year possiblity, anyway, i just finished up the DAY switch cases for the NON LEAP YEAR and wanted to put in a few COUT lines to kinda test if my switch was working, its not.

the short of it is that i cant see anything wrong with my code, can anyone tell me of any obvious flaws in the switch cases i wrote??

thank you guys in advance for helping!
code is copied from ms visual studio 2010


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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
		cout<<"Welcome, this program demonstrates the switch case"<<endl;
		cout<<"Please enter your birthday in the format mm dd yyyy"<<endl;
		cout<<"do not use any special characters: -or/ "<<endl;
		outfile<<"Welcome, this program demonstrates the switch case"<<endl;
		outfile<<"Please enter your birthday in the format mm dd yyyy"<<endl;
		outfile<<"do not use any special characters: -or/ "<<endl;
		cin>>mm>>day>>year;

		if(mm ==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
		{
		switch (day)
		{
		case '1':
		case '21':
		case '31':
			cout<<day<<"st, "<<year<<endl;
			break;
		case '2':
		case '22':
			cout<<day<<"nd, "<<year<<endl;
			break;
		case '3':
			cout<<day<<"rd, "<<year<<endl;
			break;
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '10':
		case '11':
		case '12':
		case '13':
		case '14':
		case '15':
		case '16':
		case '17':
		case '18':
		case '19':
		case '20':
		case '24':
		case '25':
		case '26':
		case '27':
		case '29':
		case '30':
			cout<<day<<"th, "<<year<<endl;
			break;
		}
		}
		if(mm==4||mm==6||mm==9||mm==11)
		{
		switch (day)
		{
		case '1':
		case '21':
			cout<<day<<"st, "<<year<<endl;
			break;
		case '2':
		case '22':
			cout<<day<<"nd, "<<year<<endl;
			break;
		case '3':
			cout<<day<<"rd, "<<year<<endl;
			break;
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '10':
		case '11':
		case '12':
		case '13':
		case '14':
		case '15':
		case '16':
		case '17':
		case '18':
		case '19':
		case '20':
		case '24':
		case '25':
		case '26':
		case '27':
		case '28':
		case '29':
		case '30':
			cout<<day<<"th, "<<year;
			break;
		}
		}
		if(mm==2)
		{
		switch (day)
		{
		case '1':
		case '21':
			cout<<day<<"st, "<<year<<endl;
			break;
		case '2':
		case '22':
			cout<<day<<"nd, "<<year<<endl;
			break;
		case '3':
			cout<<day<<"rd, "<<year<<endl;
			break;
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '10':
		case '11':
		case '12':
		case '13':
		case '14':
		case '15':
		case '16':
		case '17':
		case '18':
		case '19':
		case '20':
		case '24':
		case '25':
		case '26':
		case '27':
		case '28':
			cout<<day<<"th, "<<year;
			break;
		}
First of all, I don't know why you wrapped all your numbers in single quotes. That could be a bit counterproductive, considering that all those numbers are now characters... I'd get rid of them.

Also... I'm not quite sure why your code had to be so long... why not just remove the last two switch statements and the ifs around them and add a few ifs to quit the program if you enter the wrong number just after your cin?

-Albatross

EDIT: At the time of posting, I passed Grey Wolf... O_o
Last edited on
Not to mention that two digit numbers can't be single character literals...
albatross,

i wrapped my numbers in singl quotes because a friend told me to do it in another part of the program.
i dont understand why its counterproductive? do switch cases require characters and not INT's?

code so long? bear with me im in my 5th week and i cant see how to shorten code by checking valid input before entering switch cases. so i'll work on that today for sure! thanks

zhuge,
ok, i understand your statement. can i take a users input like 556 and write a switch case for the entire INT?

1
2
3
case(556):
    cout<<day<<"th ,"<<year<<endl;
    break;


thank you guys for pointing out my newbie mistakes!
When you put a character in single quotes, that gives the character's value (this only really works for single characters; some weird stuff might happen if you put multiple characters in single quotes). As an example, '1' translates to the value of 49. Not what you wanted? >_>

If you remove all the single-quotes, then you'll be doing comparing the actual number you had in the single quotes and whatever you put in the () of your switch, which is what you want.

What you could do for code-shortening is something like this:

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

int main () {
	int mm, day, year;
	cout<<"Welcome, this program demonstrates the switch case"<<endl;
	cout<<"Please enter your birthday in the format mm dd yyyy"<<endl;
	cout<<"do not use any special characters: -or/ "<<endl;
	cin>>mm>>day>>year;
	
	if ((mm == /*something*/ || mm == /*something else*/ || /*...*/) && day > /*some value*/)
		return 0;
	else if (/*other set of conditions*/)
		return 0;
	/*other else ifs*/
	
		switch (day)
		{
			case 1:
			case 21:
			case 31:
				cout<<day<<"st, "<<year<<endl;
				break;
			case 2:
			case 22:
				cout<<day<<"nd, "<<year<<endl;
				break;
			case 3:
				cout<<day<<"rd, "<<year<<endl;
				break;
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
			case 16:
			case 17:
			case 18:
			case 19:
			case 20:
			case 24:
			case 25:
			case 26:
			case 27:
			case 29:
			case 30:
				cout<<day<<"th, "<<year<<endl;
				break;
		}
}


...or something like that. :)

-Albatross
Last edited on
OHH okay...three if statements to filter and have only one place where the days get switched. thanks a lot, i just finished the leap year check and it seems to be functioning properly.

thanks for your time albatross
Pardon the skim, but has no one mentioned that you don't need a gigantic switch()?

Use the modulus operator to get the last digit and switch on only that.
I honestly thought about suggesting that, but then decided that I shouldn't.

The thing is that I don't know the details of what the professor might be expecting. I'd be afraid that he might not get the credit he might for using a large switch. After all, you would need an if statement to override the naming of the 12th day of the month.

Maybe I should have recommended that lines 31 through 53 be replaced with a default:, if they taught the OP about that. However, getting the program to work is priority, while getting it to look good is secondary (tertiary for me, actually). :)

@unclebenny
You can replace lines 31 through 53 (in my example) with a default: tag. Sorry for not catching that earlier. :)

-Albatross
Last edited on
@Duoas
albatross is right, getting program written properly and understanding is the goal for this class, now entering my 8th week of programming ever.
but i did end up shortening my code a whole lot thanks to albatross


thanks for your time guys
Topic archived. No new replies allowed.