Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By Cat

Function won't call function [C]

This is a discussion on Function won't call function [C] within the C Programming forums, part of the General Programming Boards category; Hello, I'm having a problem with my homework. The task is to write a program that will take time and ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Function won't call function [C]

    Hello, I'm having a problem with my homework. The task is to write a program that will take time and date typed by user, and increase it by 1 minute. I should write 3 functions - first calls second function that updates time and calls third function, if time is 00:00.
    Time update works, but date update does not.
    My whole written code:

    Code:
    #include<stdio.h>
    struct DateAndTime
        {
            struct date
                {
                    int day;
                    int month;
                    int year;
                }sdate;
            struct time
                {
                    int min;
                    int hour;
                }stime;
        }datums;
        struct time stime = {1,1};
        struct date sdate = {1,1,1};
        struct DateAndTime calc_time(struct DateAndTime);
        struct time time_update(struct time);
        struct date date_update(struct date);
    int main ()
    {
        printf("Ievadiet laiku hh:mm formata.\n");
        scanf("%d:%d", &datums.stime.hour, &datums.stime.min);
        printf("Ievadiet datumu dd/mm/yyyy formata.\n");
        scanf("%d/%d/%d", &datums.sdate.day, &datums.sdate.month, &datums.sdate.year);
    
           printf("\nIevaditais laiks ir %.2d:%.2d.\n", datums.stime.hour, datums.stime.min);
           printf("Ievaditais datums ir %.2d.%.2d.%.2d.\n", datums.sdate.day, datums.sdate.month, datums.sdate.year);
    
           calc_time(datums);
    
           printf("\nAprekinatais laiks ir %.2d:%.2d.\n", datums.stime.hour, datums.stime.min);
           printf("Aprekinatais datums ir %.2d.%.2d.%.2d.\n", datums.sdate.day, datums.sdate.month, datums.sdate.year);
    return 0;
    }
    
    struct DateAndTime calc_time(struct DateAndTime datums)
    {
        time_update(datums.stime);
        if(stime.min==00 && stime.hour==00)
        {
            date_update(datums.sdate);
        }
        return datums;
    }
    struct time time_update(struct time stime)
        {
            if(datums.stime.min>59 || datums.stime.min<0)
            {
                printf("Minutes ievaditas nepareizi.\n");
            }
            else if(datums.stime.hour>23 || datums.stime.min<0)
            {
                printf("Stundas ievaditas nepareizi.\n");
            }
            else datums.stime.min++;
            if(datums.stime.min==60)
                {
                    datums.stime.hour++;
                    datums.stime.min=00;
                }
            if(datums.stime.hour==24)
                {
                    datums.stime.hour=00;
                    datums.stime.min=00;
                }
            return stime;
        }
    struct date date_update(struct date sdate)
        {
            if(datums.sdate.day<1 || datums.sdate.day>30)
                printf("Dienas ievaditas nepareizi!\n");
            else if(datums.sdate.month<1 || datums.sdate.month>12)
                printf("Menesi ievaditi nepareizi!\n");
            else if(datums.sdate.year<1)
                printf("Gadi ievaditi nepareizi!\n");
            else
            datums.sdate.day++;
            if(datums.sdate.day==31)
                {
                    datums.sdate.month++;
                    datums.sdate.day=01;
                }
            if(datums.sdate.month==13)
                {
                    datums.sdate.year++;
                    datums.sdate.month=01;
                }
            return sdate;
        }
    P.S. I should check if inputs are numbers only, so I tried including isdigit function from ctype.h library, but that didn't work either, after I was trying it for a good hour or so, but I kinda rage quit that...

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    You are ignoring the return value of the function.
    You are doing the same thing for most of the other functions you wrote!


    Code:
    calc_time(datums);
    You likely want.

    Code:
    datums = calc_time(datums);
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    I just tried that, but when I change
    Code:
    calc_time(datums);
    to
    Code:
    datums = calc_time(datums);
    time does not update, so that must not be it. (And I wrote it the way we were taught to call functions.)
    I tried it for all functions I had, but it's the same result - nothing.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    struct time stime = {1,1};
    ...
    struct DateAndTime calc_time(struct DateAndTime datums)
    {
        time_update(datums.stime);
        if(stime.min==00 && stime.hour==00)
    You are checking the wrong struct.

    Using a debugger helps a lot with problems like that.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    How come? stime is a part of datums. I tried writing like this
    Code:
        time_update(datums.stime);
        if(datums.stime.min==00 && datums.stime.hour==00)
    but it's the same result.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,254
    Changes to arguments passed by value are never visible to the caller.

    Your functions are separately returning the changed value (using the return statement) but the caller still needs to access that returned value. So, again, you need to do "datums.stime = time_update(datums.stime)". That "datums.stime = " assignment is the mechanism by which the caller captures the returned value.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Alright, I just changed the code using the assignments.
    It didn't change anything with the date function, but time function does not work anymore...
    Code:
    datums.stime = time_update(datums.stime);
    ...
    struct time time_update(struct time stime)
        {
            if(datums.stime.min>59 || datums.stime.min<0)
                printf("Minutes ievaditas nepareizi.\n");
            if(datums.stime.hour>23 || datums.stime.min<0)
                printf("Stundas ievaditas nepareizi.\n");
            datums.stime.min++;
            if(datums.stime.min==60)
                {
                    datums.stime.hour++;
                    datums.stime.min=00;
                }
            if(datums.stime.hour==24)
                {
                    datums.stime.hour=00;
                    datums.stime.min=00;
                }
            return stime;
        }
    I just don't get it...


    EDIT: I just got it to work.. Forgot to add "datums." to "sdate.day" in 2 places. Thanks for your help, problem solved.
    Last edited by Arcca; 03-23-2013 at 04:53 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,254
    You're just getting lucky, making half-arsed guesses about how to fix your code. You have multiple variables and arguments with the same name, despite being distinct entities.

    Try rewriting your code so it does not define any global variables. Above main(), there are three defined variables named datums, stime, and sdate. Move them inside main(), before the first printf() statement.

    That will cause a couple of your functions to be rejected by the compiler. Work on fixing your code so those functions compile but DO NOT move any variables to be at file scope.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Arcca View Post
    How come? stime is a part of datums.
    There is an stime member of datums, but there is also a stime variable that has nothing to do with datums as well (likewise with sdate). You used the same variable name in different places and confused yourself on which one you're accessing.

    Further, most of your methods aren't even using the struct you're passing as a parameter - they are accessing the global variable datums, essentially making most of the methods useless (e.g. you couldn't have more than one DateAndTime object and call methods for each).
    Last edited by Cat; 03-23-2013 at 08:51 AM.
    stahta01 likes this.
    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: 3
    Last Post: 09-22-2011, 09:59 PM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  4. Replies: 2
    Last Post: 06-21-2005, 02:41 PM
  5. function call for a average function
    By ricekingg in forum C++ Programming
    Replies: 1
    Last Post: 02-04-2002, 01:16 PM

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