Thread: Simple switch case with date not working

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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