[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

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

0
akohan
Asked:
akohan
3 Solutions
 
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
 
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
Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now