Thread: help writing "switch" statement

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

    help writing "switch" statement

    Hi all,

    This is a continuation of my previous thread about the banking system that I have been working on. My professor wants us to incorporate specific dates when processing each of the options (deposits, withdrawals, etc.) in Part B of the assignment. I know that I need to use a "switch" statement, courtesy of matsp, to initiate the month and day. However, I don't seem to understand how it works. This is what I have tried to run so far:

    Code:
    int month, days;
    
    printf("Enter the transaction month (1-12): ");
      scanf("%d", month);
    
    switch (month) {
      case 1: days = 31; \\This represents the month of January.
      break;
      
      ...
    
      case 12: days = 31; \\This represents the month of December.
      break;
    }
    
    printf("Number of days in this month: %d\n\n", days);
    The actual code for the banking system is much longer than this, but I simplified it down to the part that I need help with. In addition, I did not include a "default" in my "switch" statement because I don't understand how that part is supposed to work. I have also tried following many tutorials that attempt to explain the "switch" statement, but haven't gotten much headway. With that said, I would greatly appreciate it if someone could point me in the right direction with this, including an example that I can reference as well. Thanks!
    Last edited by 3MGFX; 10-11-2007 at 03:54 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It looks right except:
    > scanf("%d", month);
    Here you need to pass the address of:
    Code:
      scanf("%d", &month);
    And:
    Code:
      case 1: days = 31; \\This represents the month of January.
    See the two backslashes? To begin a comment use two forward slashes:
    Code:
      case 1: days = 31; //This represents the month of January.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Default is used as a none-of-the-above-case. You don't need it here if you assume that the user enters a number 1-12.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I did not include a "default" in my "switch" statement because I don't understand how that part is supposed to work.
    For your switch it would be code you wanted executed for the case of an invalid month (not in the range of 1 to 12). An error handling case for when the user enters invalid input.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >See the two backslashes? To begin a comment use two forward slashes:
    I guess I should mention the other comment style (which is the only valid style under the C90 Standard):
    Code:
    /* Comment */

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    This is definately the most helpful message board I have ever signed up for. It's amazing how easy a fix this was, and I was determined that I had no idea what I was doing for about 2 1/2 hours!! LOL

    Thanks to everyone who helped me with this!

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Presuming 1 is for january and 12 is for december, there's no "real" need to use a switch statement (i agree this is the simplest way tough, and if you want to know, i may have done it that way either).
    But, here's another way (i'm throwing this just to let you know, don't mind using it)

    Code:
    if (month == 2)
        // That's the silly february month, add some special traitement to determine if it's 28 or 29 days
       days = 28;
    else if ((month + month / 8) % 2 == 1) 
       days = 31;
    else
       days = 30;
    Haha. In fact, i forgot this was "that" complex, since July (7) and August (8) have both 31 days... someone has a better "analytical way" of doing so ?
    Last edited by foxman; 10-11-2007 at 07:35 PM.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by foxman View Post
    Presuming 1 is for january and 12 is for december, there's no "real" need to use a switch statement (i agree this is the simplest way tough, and if you want to know, i may have done it that way either).
    This is probably simplest:

    Code:
    static const int days_per_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    Of course there is the matter of leap year.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 01:51 PM
  2. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  3. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM