Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This is a very commented piece of code so beginners at C/C++ can
- * hopefully understand how they work :) */
- #include <stdio.h>
- /* the struct keyword is almost like typedef. You make the base struct and give
- * it a name, then you actually use that struct to create others that inherrit
- * the same variables that are inside of it. Here we make a structure called
- * "time", that contains the hour, minute, and seconds... it holds the info for
- * all of the other structs we will declare that is of the same type. */
- struct time
- {
- int hour;
- int minutes;
- int seconds;
- };
- int main(void)
- {
- /* prototype for the function timeUpdate. It will return a struct */
- struct time timeUpdate(struct time now);
- /* This is declaring that the structs currentTime and nextTime are of
- * the type "time", so they inherit all of the qualities of the
- * time struct */
- struct time currentTime, nextTime;
- /* Get the current time and put it into the struct "currentTime" */
- printf("Enter the time (hh:mm:ss): ");
- scanf("%i:%i:%i", ¤tTime.hour,
- ¤tTime.minutes, ¤tTime.seconds);
- /* Run a function that will update the time, passing it
- * the current time, and the function will return a structure...
- * that will equal "nextTime". Makes sense if you think about it
- * remember that "nextTime" has been declared on line 22 and it has
- * all of the qualities of the global struct "time" that was declared
- * at the very beginning of the code. */
- nextTime = timeUpdate(currentTime);
- /* Of course, this is just printing out the nextTime. */
- printf("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour,
- nextTime.minutes, nextTime.seconds);
- return 0;
- }
- /* function to update the time by one second and the structure that is
- * passed to the function timeUpdate is reffered to as "now" */
- struct time timeUpdate (struct time now)
- {
- ++now.seconds;
- if(now.seconds == 60){
- now.seconds = 0;
- ++now.minutes;
- if(now.minutes == 60){
- now.minutes = 0;
- ++now.hour;
- if(now.hour == 24)
- now.hour = 0;
- }
- }
- /* You can see that this function is of the type "struct time", so it is
- * going to RETURN a structure that holds the same contents of time...
- * of course, the struct "now" has been changed by all of the if statements
- * above. (this is what makes one more second on the "clock" so to speak */
- return now;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement