Programming in C

(Barry) #1

178 Chapter 9 Working with Structures



  1. If the number of seconds reaches 60, the seconds must be reset to 0 and the min-
    utes increased by 1.

  2. If the number of minutes reaches 60, the minutes must be reset to 0 and the hour
    increased by 1.

  3. If the number of hours reaches 24, the hours, minutes, and seconds must be reset
    to 0.
    Program 9.5 uses a function called timeUpdate, which takes as its argument the current
    time and returns a time that is one second later.


Program 9.5 Updating the Time by One Second
// Program to update the time by one second

#include <stdio.h>

struct time
{
int hour;
int minutes;
int seconds;
};

int main (void)
{
struct time timeUpdate (struct time now);
struct time currentTime, nextTime;

printf ("Enter the time (hh:mm:ss): ");
scanf ("%i:%i:%i", ¤tTime.hour,
¤tTime.minutes, ¤tTime.seconds);

nextTime = timeUpdate (currentTime);

printf ("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour,
nextTime.minutes, nextTime.seconds );

return 0;
}

// Function to update the time by one second

struct time timeUpdate (struct time now)
{
++now.seconds;
Free download pdf