• C

Wrong results in C

Hi Group,

I'm running the following code in snippet. However, I get wrong number as output. I will appreciate it if you help me to find where I went wrong.

Thanks,
ak

Results:

tmp]$ ./ut
Enter the time (hh:mm:ss):1
Updated time is 134513817:1321973877:-1077819540



#include <stdio.h>
 
struct time
{
   int hr;
   int min;
   int sec;
 
};
 
 
struct time timeUpdate(struct time now);
 
 
 
int main(void)
{
 
   struct time currentTime, nextTime;
 
 
   printf("Enter the time (hh:mm:ss):");
   scanf("%i:%i:%i", &currentTime.hr,
                    &currentTime.min,
                    &currentTime.sec
        );
 
   nextTime = timeUpdate(currentTime);
 
   printf("Updated time is %.2i:%.2i:%.2i\n", nextTime.hr, nextTime.min, nextTime.sec);
 
   return 0;
}
 
 
struct time timeUpdate(struct time now)
{
   ++now.sec;
 
   if (now.sec == 60 ){
      now.sec = 0;
      ++now.min;
 
      if (now.min == 60 ){
         now.min = 0;
         ++now.hr;
 
         if (now.hr == 24)
            now.hr = 0;
     }
 
 
   }
 
}

Open in new window

akohanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LordWolfyCommented:
Your timeUpdate function isnt returning a result.

Add this line:
return now;

Open in new window

0
ozoCommented:
int main(void)
{
 
   struct time currentTime, nextTime;
 
 
   printf("Enter the time (hh:mm:ss):");
   scanf("%i:%i:%i", &currentTime.hr,
                    &currentTime.min,
                    &currentTime.sec
        );
 
   printf("Current time is %.2i:%.2i:%.2i\n", currentTime.hr, currentTime.min, currentTime.sec);


   nextTime = timeUpdate(currentTime);
 
   printf("Updated time is %.2i:%.2i:%.2i\n", nextTime.hr, nextTime.min, nextTime.sec);



 
   return 0;
}
 
 
struct time timeUpdate(struct time now)
{
   ++now.sec;
 
   if (now.sec == 60 ){
      now.sec = 0;
      ++now.min;
 
      if (now.min == 60 ){
         now.min = 0;
         ++now.hr;
 
         if (now.hr == 24)
            now.hr = 0;
     }
 
 
   }
 
   return now;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akohanAuthor Commented:

Thank you so much!
I totally had missed that line. One thing about timing in C; what should I do to get only the second part. What I'm trying to say is that let's assume you want to get the value from your machine's clock and extract the second part of it.

What should I do then?

I tried this code:

{
   time_t seconds;

   seconds = time (NULL);
   printf("%d ", seconds);
}

when I print it continously it shows the increasing value which I can guess it is from clock but the number is so big:

1202774451
1202774452
1202774453

Any idea?

regards,
ak
0
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

LordWolfyCommented:
the time function returns the number of seconds since the 1st january 1970.

Try this instead to get local time

(code example pasted from http://www.cplusplus.com/reference/clibrary/ctime/localtime.html)
/* localtime example */
#include <stdio.h>
#include <time.h>
 
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
 
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  
  return 0;
}

Open in new window

0
LordWolfyCommented:
Also, here are the details for the tm struct so you can take any part of the tiem you like from it
struct tm {
  int tm_sec;
  int tm_min;
  int tm_hour;
  int tm_mday;
  int tm_mon;
  int tm_year;
  int tm_wday;
  int tm_yday;
  int tm_isdst;
}

Open in new window

0
Infinity08Commented:
Let me get back to your original code, and give a few comments :

1) It's better to use %d instead of %i to read ints


2) As said, you have to actually return a struct time from the timeUpdate function


3) Why do you implement your own time functionality ? C has a good standard header that already does most of what you want to do :

        http://www.cplusplus.com/reference/clibrary/ctime/


4) In your timeUpdate function : why do you check using == ... Wouldn't >= be better ? Or even better, use division and modulo operations :

        min += sec / 60;
        sec %= 60;

    something like the above (and similarly for the rest of course)


5) And finally you gave this input :

        Enter the time (hh:mm:ss):1

    You should give a valid input ;) In hh:mm:ss format that is
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.