Thread: Am I doing this homework problem correctly? (Switch Statement)

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Am I doing this homework problem correctly? (Switch Statement)

    It usually takes me at least an hour to do the last problem my instructor assigns, but I got done with this one in 15 min. Since it is an online course I can't directly ask my instructor if I am doing it properly (at least I thnk I can't) and he would have to be online to check it before the deadline. So I was hoping you guys could give me second opinion on if this is correct.

    Here is the problem:

    Write and run a C program that requests both a month and a day value. Only month values between 1 and 12 should be accepted. Further, day values between 1 and 28 should be accepted for month 2 (Februrary); day values between 1 and 30 should only be accepted for months 4, 6, 9, and 11 (April, June, Spetember, and November); and only day values between 1 and 31 should be accepted for all remaning months. (Hint: Use a switch statement)

    Here is the code:

    Code:
    //Cameron Tayor
    
    #include <stdio.h>
    #define MAXCOUNT 10
    
    int main (){
    
        int opselect;
        int day, month;
    
        printf("Enter a number for the month");
            scanf("%d", &month);
        printf("Enter a number for the day of the month");
            scanf("%d", &day);
        printf("Enter a seclect code");
        printf("\n 1 for months 1-4");
        printf("\n 2 for months 5-8");
        printf("\n 3 for months 9-12");
            scanf("%d", &opselect);
    
        switch (opselect) {
        
        case 1:
            if (month == 1 && day > 0 && day < 32)
                printf("The date you have entered was January %d", day);
            else if (month == 2 && day > 0 && day < 29)
                printf("The date you have  entered was February %d", day);
            else if (month == 3 && day > 0 && day < 32)
                printf("The date you have entered was March %d", day);
            else if (month == 4 && day > 0 && day < 31)
                printf("The date you have entered was April %d", day);
            else 
                printf("The date you entered is not valid");
            break;
        case 2:
            if (month == 5 && day > 0 && day < 32)
                printf("The date you have entered was May %d", day);
            else if (month == 6 && day > 0 && day < 31)
                printf("The date you have entered was June %d", day);
            else if (month == 7 && day > 0 && day < 32)
                printf("The date you have entered was July %d", day);
            else if (month == 8 && day > 0 && day < 32)
                printf("The date you have entered was August %d", day);
            else 
                printf("The date you entered is not valid");
            break;
        case 3:
            if (month == 9 && day > 0 && day < 31)
                printf("The date you have entered was September %d", day);
            else if (month == 10 && day > 0 && day < 32)
                printf("The date you have entered was October %d", day);
            else if (month == 11 && day > 0 && day < 31)
                printf("The date you have entered was November %d", day);
            else if (month == 12 && day > 0 && day < 32)
                printf("The date you have entered was December %d", day);
            else 
                printf("The date you entered is not valid");
            break; }
    
    
    return 0;
    }
    It works just fine, but I don't want to be too secure if I might of missed something. The chapter we are doing just went finished going over for and while loops. So it doesn't fit with the chapter, but asks specifically for a switch statement.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: The switch statement is very unlikely to be driven as you wrote it. Likely it is supposed to switch on the month value.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by stahta01 View Post
    FYI: The switch statement is very unlikely to be driven as you wrote it. Likely it is supposed to switch on the month value.

    Tim S.
    You are saying that I should make a "case" for each month?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Cameron Taylor View Post
    You are saying that I should make a "case" for each month?
    I would do it that way.
    No where in the problem you posted did it say ask for a third input value as you wrote it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by stahta01 View Post
    I would do it that way.
    No where in the problem you posted did it say ask for a third input value as you wrote it.

    Tim S.
    Does this look like what you imagined?

    Code:
    //Cameron Tayor
    
    #include <stdio.h>
    
    
    int main (){
    
        int opselect;
        int day, month;
    
        printf("\nEnter a number for the month: ");
            scanf("%d", &month);
        printf("Enter a number for the day of the month: ");
            scanf("%d", &day);
    
        switch (month) {
        
        case 1:
            if (day > 0 && day < 32)
                printf("The date you entered was January %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 2:
            if (day > 0 && day < 29)
                printf("The date you entered was February %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 3:
            if (day > 0 && day < 32)
                printf("The date you entered was March %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 4:
            if (day > 0 && day < 31)
                printf("The date you entered was April %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 5:
            if (day > 0 && day < 32)
                printf("The date you entered was May %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 6:
            if (day > 0 && day < 31)
                printf("The date you entered was June %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 7:
            if (day > 0 && day < 32)
                printf("The date you entered was July %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 8:
            if (day > 0 && day < 32)
                printf("The date you entered was August %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 9:
            if (day > 0 && day < 31)
                printf("The date you entered was September %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 10:
            if (day > 0 && day < 32)
                printf("The date you entered was October %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 11:    
            if (day > 0 && day < 31)
                printf("The date you entered was November %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        case 12:
            if (day > 0 && day < 32)
                printf("The date you entered was December %d\n", day);
            else     printf("The day you entered is not valid\n");
            break;
        default:
                printf("The month you entered is not valid\n");
            break; }
    
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    That looks better to me.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help! changing from a switch statement to if-else
    By Rachel Hyde in forum C Programming
    Replies: 3
    Last Post: 03-07-2013, 05:31 PM
  2. Having a problem with a Switch Statement
    By ARod609 in forum C Programming
    Replies: 15
    Last Post: 07-13-2011, 06:57 PM
  3. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  4. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM