Thread: How can I turn this code into a case switch statement?

1. How can I turn this code into a case switch statement?

I need to turn this code into a case switch statement. Tried several times to change it but no luck. Anybody can help and point me to the right direction?
Code:
```int leap (int year)
{
int leapyer;

if ((year&#37;4==0) && (year%100!=0) || (year%400==0))
leapyer=366;
else
leapyer=365;

return leapyer;
}```

2. Why would you want to do that ?

3. Your condition is off: a year is a yeap year iff

(year divisible by 4) AND ((year not divisible by 100) OR (year divisible by 400))

4. Yeah I changed the code from original to fit a function structure and I've prolly made a mistake. I changed it but it requires that (year%100!=0) not (year%100!==0) //extra "=" gives an error. Anybody knows how to make this into a case switch?

5. I tried this and it gives me an error at the begining saying: expected unqualified-id before "switch"
expected `,' or `;' before "switch"
this is what I wrote
Code:
```			switch (int leapyer)
{

case: ((year&#37;4==0) && (year%100!=0) || (year%400==0));
leapyer=366;
break;
default:
leapyer=365;
break;
}```

6. Why do you want to switch to a switch statement?

The logic of the original code does not lend itself well to a switch.

7. because the assignment needs a switch and out of the entire program this part seems to be the most easy to change into a switch.

8. I don't think so. This would be very difficult to change to a switch. A switch needs one value to key on, you could use year. It also needs constant values for the cases. So year&#37;4 doesn't work for that. You'd have to have a case for every possible year. You might be able to use three switch cases, one for year%4, one for year%100 and one for year%400, but that makes very little sense.

Do you have a menu somewhere in the program perhaps? Anything else that has options based on a number?

9. This asks for year. Its the begining of program. Its part of a do while loop.
Code:
```cout<<"Enter staring month ";
cin>>mnth1;
cout<<"Enter starting day ";
cin>>day1;
cout<<"Enter starting year ";
cin>>yer1;
first_date=days(yer1,mnth1,day1);      //days function
cout<<"Enter ending month ";
cin>>mnth2;
cout<<"Enter ending day ";
cin>>day2;
cout<<"Enter ending year ";
cin>>yer2;```

10. Originally Posted by Lonck
because the assignment needs a switch and out of the entire program this part seems to be the most easy to change into a switch.
This is probably the worst possible choice to use a switch, actually.

11. The only kind of if you can turn into a switch/case is the if ( var == const ) variety, and even then the only constants allowed are character and integer constants.

Perhaps post your whole code and we can tell you other alternatives which might be made switchable to contrive your project requirement.

12. Days per month is a typical case for using switch(). Remember february is special. [Of course, it's more efficient to use an array with 12 entries, and only an if-statement for february].

--
Mats

13. You can devise a switch like this:

Code:
```switch ( number )
{
case 1:
if ( code )
{
// do somthing
}
break;
}```
Sort if nesting the if within a switch. But then again this is also pointless and you can achirve the same thing and more fessable using if else if or like matsp said an array.