Thread: wierd results

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    wierd results

    How come this is not returning the desired outcome?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char **argv) {
            time_t c_time, a_time;
            char buffer[256];
            struct tm a_time_struct;
    
            printf("Enter the time in which you want to wake up.\n");
            printf("In this format: (MM DD YYYY hh mm).\n");
            fgets(buffer, sizeof buffer, stdin);
            sscanf(buffer, "%i %i %i %i %i", &a_time_struct.tm_mon, &a_time_struct.tm_mday, &a_time_struct.tm_year
                                            ,&a_time_struct.tm_hour, &a_time_struct.tm_min);
            printf("%i = mon, %i = day, %i = year, %i = hour, %i = min.\n", a_time_struct.tm_mon, a_time_struct.tm_mday, a_time_struct.tm_year, a_time_struct.tm_hour, a_time_struct.tm_min);
    /* . . . */
    return 0;
    }
    Here is my test run.

    Enter the time in which you want to wake up.
    In this format: (MM DD YYYY hh mm).
    03 09 2004 10 56
    3 = mon, 0 = day, 9 = year, 2004 = hour, 10 = min.
    Last edited by chrismiceli; 03-08-2004 at 11:19 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps you would have better results with this?
    Code:
    sscanf(buffer, "%02d%02d%04d%02d%02d",
                   &a_time_struct.tm_mon,
                   &a_time_struct.tm_mday,
                   &a_time_struct.tm_year,
                   &a_time_struct.tm_hour,
                   &a_time_struct.tm_min);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    and apply the same solution as above to printing the output too.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    figured it, when looking at your code, I saw you used %d, not %i. I told myself a long time ago from a problem like this never to use %i, but I fell back into. Thanx for the help.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I told myself a long time ago from a problem like this never to use %i
    With the printf family %d and %i are the same. This breeds confusion because the scanf family makes an important but subtle distinction between the two. For the scanf family, %d reads a signed decimal value equivalent to a strtol call with 10 as the base. %i on the other hand, reads a signed integer value equivalent to a strtol call with 0 as the base. In other words, %i reads hexadecimal and octal as well as decimal. %i is choking on 09, an invalid octal value, so it takes input to mean that the 0 and 9 are distinct values and reads them separately, resulting in your odd output.

    So the thing to remember is not that you should avoid %i, but that you should recognize and understand the differences between similar flags in the scanf and printf families.
    My best code is written with the delete key.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thanx for the distinction, I always used to just remembered to avoid %i.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  2. OpenGL Wierd **** - Order of Drawing Stuff?
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 11-09-2006, 09:56 PM
  3. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM