Thread: How To Set An Integer To 00?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    How To Set An Integer To 00?

    Hello, well I'm attempting to make a clock. I have set, for example, the seconds area to a loop to go from 0 - 59 and then back to 0 again. The problem is I want to set it so it looks like this:

    1:55:59

    to

    1:56:00

    The problem is when the seconds counter resets, it looks like this.

    1:56:0

    With only one zero. How do i code it to set the integer to 00, because every time I try, for example: z = 00; it just prints the digit as "0". If anyone can help that would be great. Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    printf("%02d\n",0);

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Show us your code...

    It's probably a simple matter of adjusting a couple of printf() statements, but none of us are mind readers.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Sorry about this. Here is all the code I have so far.

    Code:
    #include <cs50.h>
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    
    int
    main(int argc, char * argv[])
    {
        int x, y, z, r;
        r = 0;
        printf("Please select the hour: ");
        x = GetInt ();
        printf("Minutes: ");
        y = GetInt ();
        printf("Seconds: ");
        z = GetInt ();
        printf("The time is: %d:%d:%d\r", x, y, z);
        Sleep(1000);
        for(r; r == 0; r)
        {
            if (z == 59)
            {
                z = 00;
                if (y == 59)
                {
                    y = 00;
                    if (x == 12)
                    {
                        x = 1;
                    }
                    else
                    {
                        x++;
                    }
                }
                else
                {
                    y++;
                }
                printf("                     \r");
                printf("The time is: %d:%d:%d\r", x, y, z);
                Sleep(1000);
            }
            else
            {
                z++;
                printf("The time is: %d:%d:%d\r", x, y, z);
                Sleep(1000);
            }
        }
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You could simplify this code quite a bit by using modulous (%) to limit the range of your variables. Even further by using the localtime() function.

    Your display problem can most easily be fixed by using "%2d" in your printf() statments.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    An integer simply stores a whole number quantity. How you display that quantity is up to you.

    You can show as many leading zeros as you like and fill it with commas, decimal points, dollar sign or whatever else you want to format into the resulting string.

    You can't make an acurate clock by using 'Sleep'. You perhaps want to ask the OS for the time.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is malloc doing?
    By tpe in forum C Programming
    Replies: 24
    Last Post: 11-19-2010, 12:49 PM
  2. Is this a timestamp?
    By NewnOT in forum Tech Board
    Replies: 11
    Last Post: 09-21-2010, 03:41 PM
  3. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  4. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  5. Problem using fwrite
    By kenkoh in forum C Programming
    Replies: 10
    Last Post: 02-16-2008, 10:34 AM