# Calendar problem

Can anyone help me with this, most probably easy problem to must of you.
"Write a program that uses a combination of a for statement and a switch statement to loop over the months of this year, labelled by 1,2...12, and print the number of days i.e. 30 for Apr.,Jun.,Sep. and Nov., 29 for Feb. and 31 for the rest. You will need two case statements
and a default: Your output can be of the form:
Month 1 has 31 days
Month 2 has 28 days
..."

What I really need to know is how can I write a for loop which points specific numbers to a certain switch case, i.e, if when x=7 I want it to output case 2. Here is a sample of the code I have wrote

"
//
// Calendar for the year
//

#include
#include

int main()
{

for(int m=1; m<=12; m++)
{
cout<<"month "<<m;

switch(m)
{

case 1:
cout<<" has 31 days"<<endl;
break;

case 2:
cout<<" has 30 days"<<endl;
break;

default:
cout<<" has 28 days"<<endl;
}
}
return 0;
}
"

I have even tried using the math function || for the cases but was unsuccessful. I'm sure there is something simple I'm missing, so I would be grateful for some help.

Thanks

Matthew

• [b][red]This message was edited by stober at 2005-2-28 19:27:53[/red][/b][hr]
The loop and switch statement are basically correct as posted. All you need to do is add the other cases, 3-12. You might want to add comments to make it easier for you to remember which numbers represent which months (I do that because I always forget).

[code]
: "
: //
: // Calendar for the year
: //
:
: #include
: #include
:
: int main()
: {
:
: for(int m=1; m<=12; m++)
: {
: cout<<"month "<<m;
: switch(m)
: {
:
: // months that contain 30 days --
// 30 days has Sept, Apr, Jun and Nov
: case 9: //Sep
case 4: // Apr
case 6: // Jun
case 11: // Nov

: cout<<" has 30 days"<<endl;
: break;
:
: case 2: // February
if( (year%4) == 0)
: cout<<" has 28 days"<<endl;
else
: cout<<" has 29 days"<<endl;
: break;
:
// All the rest have 31 days
: default:
: cout<<" has 31 days"<<endl;
: }
: }
: return 0;
: }
[/code]: "
:
: I have even tried using the math function || for the cases but was unsuccessful. I'm sure there is something simple I'm missing, so I would be grateful for some help.
:
: Thanks
:
: Matthew
:

• Thanks for your help. What I was trying to do with the switch statement was something like this:

case 9||4||6||11: \ Sept, april Jun and Nov
cout<<" has 30 days"<<endl;
break;

I thought that I could use the logical expression "or(||)" in a case for a switch statement. As it happens, this is not possible. Again thanks for your help

Best Regards

Matt

• : Thanks for your help. What I was trying to do with the switch statement was something like this:
:
: case 9||4||6||11: \ Sept, april Jun and Nov
: cout<<" has 30 days"<<endl;
: break;
:
:
: I thought that I could use the logical expression "or(||)" in a case for a switch statement. As it happens, this is not possible. Again thanks for your help
:

Nope -- you can't do it that way. Do it the way I showed you in my previuos post.