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

This is a discussion on How can I turn this code into a case switch statement? within the C++ Programming forums, part of the General Programming Boards category; I need to turn this code into a case switch statement. Tried several times to change it but no luck. ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    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%4==0) && (year%100!=0) || (year%400==0))
             leapyer=366;
           else  
             leapyer=365;
    
         return leapyer;                        
    }
    Last edited by Lonck; 11-12-2007 at 06:23 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why would you want to do that ?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    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?
    Last edited by Lonck; 11-12-2007 at 06:23 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    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%4==0) && (year%100!=0) || (year%400==0));
                               leapyer=366;
                               break;
                       default:
                                leapyer=365;
                                break;
                  }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    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. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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%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. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    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. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by Lonck View Post
    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. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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.
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 04:06 AM
  4. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21