switch statement problems: beginner in C

This is a discussion on switch statement problems: beginner in C within the C Programming forums, part of the General Programming Boards category; hey guys, first time poster so let me know if I violate any basic rules, don't want to step on ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    switch statement problems: beginner in C

    hey guys, first time poster so let me know if I violate any basic rules, don't want to step on any toes. I'm taking a basic C programming class, and I'm having trouble with the following switch statement.
    if the problem isn't in here, it's got to be in the rest of my program. I run a printf to make sure my r is correct in output and it feeds me -12093. I was just wondering if I was making some stupid mistake in my syntax here. thanks.
    Code:
    void daysinmonth(int p, int r)
    {
    
    
    switch(p)
    {
    case 1:
    r=31;
    break;
    case 2:
    r=28;
    break;
    case 3:
    r=31;
    break;
    case 4:
    r=30;
    break;
    case 5:
    r=31;
    break;
    case 6:
    r=30;
    break;
    case 7:
    r=31;
    break;
    case 8:
    r=31;
    break;
    case 9:
    r=30;
    break;
    case 10:
    r=31;
    break;
    case 11:
    r=30;
    break;
    case 12:
    r=31;
    break;
    }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) Learn to indent your code properly... there's no excuse for not doing it.

    2) What if p is not in the range of 1 to 12? lets say some wag enters month 0 on you... what then?

    3) Try to pick more meaningful names for your variables... like month and days... It will make your code a lot easier to troubleshoot and maintain.

    4) Instead of void... your function should return the number of days... the r value inside your function is a copy of the r value outside. Changing it inside the function does not change the original.

    5) It is easy to simplfy this function quite a lot...
    Code:
    int daysinmonth(int month)
    {
       switch(month)
        {
          case 1 :
          case 3 :
          case 5 :
          case 7 :
          case 8 :
          case 10 :
          case 12 :
             return 31;
          case 2 :
             return 28;
          case 4 :
          case 6 :
          case 9 : 
          case 11 :
            return 30;
          default :
           return -1;
       }
    }
    
    // call as...
    nDays = daysinmonth(ThisMonth)
    if(nDays < 0)
      { 
         printf("ERROR : Invalid month specified\n");
         exit(1);
       }
    Last edited by CommonTater; 11-20-2011 at 03:30 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    I've done my best to implement your changes. I do my best to indent properly, but as this is my first time posting code online, I wasn't sure how to go about doing it in that format, I'll try to get it right below.

    I've changed my variables, and that has helped a ton.

    I rewrote the switch statement, and I'm now getting a 0 as the output, though I believe this is probably becuase I am not calling the function correctly. you see, this is a small part of a large assignment, and I need to use this value later to determine that number of days between to calendar dates in different years. I'll post the rest of my code, feel free to help if you want, but you've already helped a ton.
    Code:
    #include <stdio.h>
    int days, month, year, days_1, month_1, year_1;
    
    int date(int days, int month, int year)/*this is the function that allows the user to input the information they need analyzed into the program*/
    {
    int days_1, month_1, year_1;
    char a, b, a1, b1;
    printf("intput a date in the follwing format:\n");
    printf("mmddyyyy: in other words the two digit day,two digit month,\n");
    printf("and four digit year, with no slashes or heifens anywhere.\n");
    scanf("%d%c%d%c%d", &month, &a, &days, &b, &year);
            if(month<0 || month>12 || days<0 || days>31 || year<0)
            {
            printf("data not valid");
            }
    printf("enter the later date in the same format:\n");
    scanf("%d%c%d%c%d", &month_1, &a1, &days_1, &b1, &year_1);
    return days, month, year, days_1, month_1, year_1;
    }
    int daysinmonth(int month) /*there is a problem in this statement, your printf is reading out ridiculous numbers for r, don't forget to ask*/
    {
            switch(month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    return 31;
            case 2:
                    return 28;
            case 4:
            case 6:
            case 9:
            case 11:
                    return 30;
            }
    }
    /*void leap(int r)
    {
    if( r%4==0 && r%100 !=0*/
    main(void)
    {
    date(days, month, year);
    daysinmonth(month);
    printf("days in month=%d", days);
    }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You did not implement the default: case which should give you -1 on errors.

    line 47 has to read..
    Code:
    days = daysinmonth(month);
    There's no magic, it does exactly what you tell it to do...

    line 44 ... the correct form of main is .... int main (void) ...
    line 49 ... main returns an integer to the OS... you did not. ... return 0; (and yes it matters)

    line 18... you cannot return multiple values from a function.
    C returns a single value ... int, pointer, char, float etc. when a function returns. This is probably the source of your error so you need to rethink that function. I would do this with a struct to hold the inputs, creat a copy inside the function and return it to a copy outside the function when the function completes.

    If your inputs don't need to be in a function (probably smarter) I'd just do the whole thing in main with discrete variables.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Great! Thank you so much, I've now got my switch statement working. I am now attempting to write a function that tells me the time difference in two dates in the same year, so I can then multiply this by the number of years+leaps to get the total number of days between the two given dates. This has proven very difficult.

    The only way I can see to do this is to use the function difftime, but I can't find much information on it anywhere. I know it gives the difference in seconds(of two dates I believe?) I would appreciate a brief explanation on how this function works though, or if there's a simpler way to accomplish the task. Here's what my code looks like now, and whether or not you choose to continue helping me, you're F-ing awesome tater.
    Code:
    
    #include <stdio.h>
    #include <time.h>
    int days, month, year, days_1, month_1, year_1, leapy, ndays;
    
    
    
    
    int daysinmonth(int month) /*there is a problem in this statement, your printf is reading out ridiculous numbers for days, don't forget to ask*/
    {
            switch(month)
            {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                    return 31;
            case 2:
                    return 28;
            case 4:
            case 6:
            case 9:
            case 11:
                    return 30;
            default:
                    return -1;
            }
    }
    
    
    int leap(int year)
    {
            if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
            {
            leapy=1;
            }
            else
            {
            leapy=0;
            }
    }
    
    
    /*int daysnyear(int days, int month, int year)
    {*/
    int main(void)
    {
    int days_1, month_1, year_1;
    char a, b, a1, b1;
    printf("intput a date in the follwing format:\n");
    printf("mmddyyyy: in other words the two digit day,two digit month,\n");
    printf("and four digit year, with no slashes or heifens anywhere.\n");
    scanf("%d%c%d%c%d", &month, &a, &days, &b, &year);
            if(month<0 || month>12 || days<0 || days>31 || year<0)
            {
            printf("data not valid");
            }
    printf("enter the later date in the same format:\n");
    scanf("%d%c%d%c%d", &month_1, &a1, &days_1, &b1, &year_1);
    
    
    ndays=daysinmonth(month);
    printf("days in month=%d\n", ndays);
    leap(year);
    printf("%d", &leap);
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Statement Help
    By ShiroAzure in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2011, 09:40 PM
  2. switch statement problems
    By -EquinoX- in forum C Programming
    Replies: 5
    Last Post: 10-23-2008, 09:46 PM
  3. Switch statement problems
    By BSmith4740 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2008, 11:21 PM
  4. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 01:49 AM
  5. Switch statement problems
    By ComDriver in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2005, 11:31 AM

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