Thread: Date and Time Functions

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15

    Date and Time Functions

    Ok so I'm working on program and I was just wondering how i can

    make the compiler call time or date or just the year

    i want to convert date of birth to age in years any help with that?

    i under stand i need the current year Minus the Dob Year.. but im not sure how to get the program to call the date or time or even the current year alonw

  2. #2
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15
    Quote Originally Posted by MrNerd View Post
    Ok so I'm working on program and I was just wondering how i can

    make the compiler call time or date or just the year

    i want to convert date of birth to age in years any help with that?

    i under stand i need the current year Minus the Dob Year.. but im not sure how to get the program to call the date or time or even the current year alonw


    IGNORE THIS

    i fixed the problem i just used user input


    and now my program crashes after it ask's for the date..

    Code:
    //Mrnerd's DOB to Days in Age Coverter.
    
    
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    main()
    {
    int year, dobd,dobm,doby,date,todd,todm,tody;
    char y, n, again , dummy;
    
    
    
    
    y == 'y';
    n == 'n';
    
    
    
    
        printf("\t\t\t\nWhat is your Date of Birth?\n(mm - dd - yyyy)\n ");
    
    
            scanf("\n%d %d %d", &dobm, &dobd, &doby);
    
    
        printf("\t\t\t\nYou entered %d - %d - %d is this correct?\n" , dobm, dobd, doby);
    
    
    
    
            scanf("\t\t\n%c", &y);
    
    
    if (y == 'y')
    {
    
    
    printf("\t\t\t\nWhat is TODAYS's DATE?\n(mm - dd - yyyy)\n ");
    
    
      scanf("\n%d %d %d", todm, todd, tody);
    
    
        printf("\t\t\t\nYou entered %d - %d - %d is this correct?\n" , todm, todd, tody);
    
    
            scanf("\t\t\n%c", &y);
    
    
    }
    
    
    int age = doby - tody;
    
    
    int days = age * 365;
    
    
    
    
    if (y == 'y')
    {
                printf("\n You are %d Years old.\n",age);
    
    
    
    
    
    
    
    
                printf("\n That is %d Days old\n",days);
    }
    
    
    exit(0);
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    I must say that you really need to format your code properly. Use a level of indentation for each code block, and avoid having more than two blank lines. One blank line will usually suffice to separate logical portions of code. For example:
    Code:
    //Mrnerd's DOB to Days in Age Coverter.
    
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>
    
    main()
    {
        int year, dobd, dobm, doby, date, todd, todm, tody;
        char y, n, again, dummy;
    
        y == 'y';
        n == 'n';
    
        printf("\t\t\t\nWhat is your Date of Birth?\n(mm - dd - yyyy)\n ");
        scanf("\n%d %d %d", &dobm, &dobd, &doby);
    
        printf("\t\t\t\nYou entered %d - %d - %d is this correct?\n" , dobm, dobd, doby);
        scanf("\t\t\n%c", &y);
    
        if (y == 'y')
        {
            printf("\t\t\t\nWhat is TODAYS's DATE?\n(mm - dd - yyyy)\n ");
            scanf("\n%d %d %d", todm, todd, tody);
    
            printf("\t\t\t\nYou entered %d - %d - %d is this correct?\n" , todm, todd, tody);
            scanf("\t\t\n%c", &y);
        }
    
        int age = doby - tody;
        int days = age * 365;
    
        if (y == 'y')
        {
            printf("\n You are %d Years old.\n",age);
            printf("\n That is %d Days old\n",days);
        }
    
        exit(0);
    }
    Note that the return type of main should be explicitly declared as int. Then, these two statements have no net effect:
    Code:
    y == 'y';
    n == 'n';
    The former compares the uninitialised variable y with 'y' then does nothing with the result, so it might as well not have been written.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Hazel Park, Michigan, United States
    Posts
    15
    ok thanks for the tip...and sorry i do that for my own separation. so how does that make the program crash exactly?

    ok so i did what you said removed the y and n

    and now as soon i enter todays date it still crashes?

    Code:
    //Mrnerd's DOB to Days in Age Coverter.
    
    
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    main()
    {
    int year, dobd,dobm,doby,date,todd,todm,tody;
    char y, n, again , dummy;
    
    
        printf("\t\t\t\nWhat is your Date of Birth?\n(mm - dd - yyyy)\n ");
            scanf("\n%d %d %d", &dobm, &dobd, &doby);
        printf("\t\t\t\nYou entered %d - %d - %d is this correct?\n" , dobm, dobd, doby);
            scanf("\t\t\n%c", &y);
            
    if (y == 'y')
    {
    printf("\t\t\t\nWhat is TODAYS's DATE?\n(mm - dd - yyyy)\n ");
            scanf("\n%d %d %d", todm, todd, tody);
    printf("\t\t\t\nYou entered %d - %d - %d is this correct?\n" , todm, todd, tody);
            scanf("\t\t\n%c", &y);
            }
    
    
    int age = doby - tody;
    int days = age * 365;
    
    
                printf("\n You are %d Years old.\n",age);
                printf("\n That is %d Days old\n",days);
    
    
    exit(0);
    }
    Last edited by MrNerd; 10-20-2012 at 12:16 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You still don't seem to have learnt anything about properly formatting, despite being given a perfect example of how to do it.

    Please understand that to us proper code formatting is not just a nice-to-have. It is vital for us and you to see exactly what the code does and not make silly mistakes when interpreting it. You've had 15 posts here, you should know by now, so do it please.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Compiling your code with warnings I get:
    Code:
    $ gcc -ggdb3 -Wall -Wextra -o test test.c
    test.c:6:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    test.c: In function ‘main’:
    test.c:20:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
    test.c:20:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]
    test.c:20:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat]
    test.c:9:20: warning: unused variable ‘dummy’ [-Wunused-variable]
    test.c:9:12: warning: unused variable ‘again’ [-Wunused-variable]
    test.c:9:9: warning: unused variable ‘n’ [-Wunused-variable]
    test.c:8:26: warning: unused variable ‘date’ [-Wunused-variable]
    test.c:8:5: warning: unused variable ‘year’ [-Wunused-variable]
    test.c:20:14: warning: ‘todm’ may be used uninitialised in this function [-Wuninitialized]
    test.c:20:14: warning: ‘todd’ may be used uninitialised in this function [-Wuninitialized]
    test.c:20:14: warning: ‘tody’ may be used uninitialised in this function [-Wuninitialized]
    Your main problem is line 20 (line 24 in your post):
    Code:
    scanf("\n%d %d %d", todm, todd, tody);
    Can you see what's wrong with that line (compare it with line 17)?


    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ date time
    By rahulsk1947 in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2007, 02:06 PM
  2. Date & Time
    By g4j31a5 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2006, 09:25 AM
  3. Date and Time
    By Calavera in forum C Programming
    Replies: 3
    Last Post: 11-26-2004, 03:56 PM
  4. Getting time and date
    By winsonlee in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2004, 11:31 PM
  5. Date and time functions?
    By Monte in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2002, 06:15 AM