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
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
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:
#include <iostream>
usingnamespace 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;
elseif (/*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;
}
}
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.
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. :)
@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