Thread: Simple switch case with date not working

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    7

    Simple switch case with date not working

    I am having a problem type casting my strftime buffer to work with my switchcase statement. Switchcase only uses string/char/int variable, However stftime is something along the lines of a date type variable, so it's not working with my switch case statement.

    In short, I need to convert my strftime buffer variable into a char variable to print the correct day of week.


    Code:
    #include <stdio.h>     
    #include <time.h>       /* time_t, struct tm, time, gmtime */
    
    int main ()
    {
      // Get a tm structure
      time_t now = time(NULL); 
      struct tm *tick_time = localtime(&now);
      
      // Create a long-lived Time buffer
      static char buffer[0];  
      strftime(buffer, sizeof("0"), "%u", tick_time); 
    
     
      char p = atoi(buffer);
      
       switch(p)
       {
       case '1' :
          printf ("mo");
          break;
       case '2' :
           printf("tu");
          break;
       case '3' :
          printf("wed");
          break;
       case '4' :
          printf("th");
          break;
       case '5' :
          printf("fr");
          break;
        case '6' :
          printf("sa");
          break;
         default :
           printf("su \n");
       }
    
    printf(buffer);
        
    
      return 0;
    }
    Help would be greatly appreciated.
    Last edited by Leston Yearwood; 09-03-2015 at 09:28 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You're converting a string to an int, and then comparing that int to the character representation of the number. Remove the single quotes in your case statements, and you're likely to see better results.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    Thanks so much man, It works now. I am new to c so I didnt even understand you at first, but now realize that I mistook the altoi function as an int to char function for some reason. I literally took days trying to figure out this simple problem, lol.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    How much space are you allocating to your buffer? Zero? Writing anything to that array leads to undefined behavior.

    And sizeof("0") doesn't do anything particularly meaningful in this case. You should instead pass in the actual size of your buffer (which is zero as I mentioned but should be larger), not some other arbitrary size.

  5. #5
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    This was just a simple example of a much larger code what I was trying to acheive. I corrected the problem in the actual code so it was something more like.
    Code:
    #include <stdio.h>     
    #include <time.h>       /* time_t, struct tm, time, gmtime */
    
    
    int main (){
      // Get a tm structure
      time_t now = time(NULL); 
      struct tm *tick_time = localtime(&now);
       
      // Create a long-lived Time buffer
      static char buffer[]="0";  
      //Store Day number in buffer variable
      strftime(buffer, sizeof("0"), "%U", tick_time); 
    
      //Convert date variable (buffer) to an Integer 
      int wkday = atoi(buffer);
    
     //Print result according to day number (1-7)
     switch(wkday) {
       case 1 :
          printf ("mo \n");
          break;
    
       case 2 :
           printf("tu \n");
          break;
    
       case 3 :
          printf("wed \n");
          break;
    
       case 4 :
          printf("th \n");
          break;
    
       case 5 :
          printf("fr \n");
          break;
    
        case 6 :
          printf("sa \n");
    
          break;
         default :
           printf("su \n");
       }
    
      //Print Buffered Result (1-7)
      printf(buffer);
    
      return 0;
    }
    Since I only needed the one char for decision making.
    Last edited by Leston Yearwood; 09-04-2015 at 06:23 PM.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    You can also do the same using tick_time->tm_wday without using strftime at all. It's the number of days since Sunday (0 is Sunday).

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Also, in this case, I'd use a lookup table and not a switch statement, it's a lot more compact and less prone to error (like forgetting a break or something). It also is more maintainable, in that if you wanted to change what followed the abbreviation - for example to add two newlines instead of one, or to add the day of the month, or something - you can do it in one place and not seven.

    Lookup tables can be better performing than switch statements, although I don't consider that a compelling reason to use them in most cases; I prefer this method because of readability and maintainability.

    Since the values are integers, with each integer having a representation (i.e. it's not a sparse mapping) a simple array can work ideally.

    Also, for myself, I'd probably settle for the abbreviations that strftime() gives, rather than rolling my own, but if I really had to have my own day-of-week abbreviations, I'd use one of the following, either of which can accept sunday being either 0 or 7:

    Code:
    char * day_abbrev[] = {"su", "mo", "tu", "we", "th", "fr", "sa", "su"};
    printf("%s \n",day_abbrev[wkday]);
    Code:
    char * day_abbrev[] = {"su", "mo", "tu", "we", "th", "fr", "sa"};
    printf("%s \n",day_abbrev[wkday%7]);
    Last edited by Cat; 09-05-2015 at 11:06 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-30-2013, 02:32 PM
  2. switch-case statement not working properly
    By Shinzu911 in forum C Programming
    Replies: 11
    Last Post: 04-25-2012, 10:41 AM
  3. C: Default case in switch not working
    By LunaLux in forum C Programming
    Replies: 5
    Last Post: 04-24-2011, 08:46 AM
  4. case switch not working
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 06:42 AM
  5. Replies: 11
    Last Post: 08-25-2008, 12:01 PM