Thread: C++ Clock.

  1. #1
    deletedforumuser
    Guest

    C++ Clock.

    Hello, i have learned a bit more about ctime and loops.

    If any of you needed a digital clock code. Here it is if not then it's okay, because i know this script is for beginners.

    Here's a way to do it:


    Code:
    #include <iostream>
    #include <windows.h>
    
    
    int main()
    {
        using namespace std;
      
    
    	int minutes = 0; // set the minutes
    	int hours = 5; //set the hour
    
    Loop:
       for (int a = 0; a < 61; a++)
       {
    
    	   	  if (a == 60)
    		{
    			minutes = minutes + 1;
    			a = a - 60;
    			goto Loop;
    		}
    		  if (minutes == 60)
    		  {
    			  hours = hours + 1;
    			  minutes = 0;
    			  goto Loop;
    		  }
    		  if (hours == 13)
    		  {
    			  hours = hours - hours;
    			  hours = hours + 1;
    		  }
    
    
    	   	system("cls");
    		cout <<"Hours:"<<hours<<"/ Minutes:"<<minutes<<"/ Seconds:"<<a;
    	    Sleep(1000);
    
    
    	}
    
         
    	
    
    	cin.get();
    
    	return 0;
    
    }

    This way will just help you learn more about loops and how digital clocks loops works.

    Here's a more useful way to do it, to set the time to your computer time.

    Code:
    #include <ctime>
    #include <iostream>
    #include <windows.h>
    
    
    int main()
    {
    	using std::cin;
    
    	bool loop = true;
    
    
    	while (loop)
    	{
    
    
      std::time_t now = std::time ( 0 );
      std::tm *local = std::localtime ( &now );
    
      local->tm_hour -= 6;
      
      system("cls");
      std::cout << ctime ( &now ) <<'\n';
      Sleep(1000);
      
    	}
      cin.get();
    
      return 0;
    
    
    }
    Thank you, hope you find it useful, actually, for the people who just started c++.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be easier (and more readable) to just count in seconds and convert it to a more friendly format.
    You can also get rid of the "Loop" label, since you never use it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    deletedforumuser
    Guest
    I love programming this way...But it's okay if you give me your opinions, because, it helps me learn more. I'll try what you just told me. Thanks Elysia.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Now I see you're using goto. Goto is considered bad practice.
    You can use continue to jump to the top of the loop. I'm afraid you'll have to change how your code works, as well. The goto combined with the rest explains why the code is so hard to understand.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    deletedforumuser
    Guest
    Actually, it's my first time using Goto...I don't like it much...So i won't use it again.

    And by the way? Elysia, how long have you been programming?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >		  if (hours == 13)
    >		  {
    >			  hours = hours - hours;
    >			  hours = hours + 1;
    >		  }
    I take it:
    Code:
    		  if (hours == 13)
    		  {
    			  hours = 1;
    		  }
    is too easy to read.
    Code:
    >  local->tm_hour -= 6;
    This looks like a stray line you forgot to delete.

  7. #7
    deletedforumuser
    Guest
    Thank you swoopy, i could've made it this way:

    Code:
    if (hours == 13)
    		  {
    			  hours = 1;
    		  }
    But, i forgot about it, i'm just having a headache right now, been reading the book for hours now.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    Loop:
       for (int a = 0; a < 61; a++)
       {
    
    	   	  if (a == 60)
    		{
    			minutes = minutes + 1;
    			a = a - 60;
    			goto Loop;
    		}
    You are obfuscating your code - either on purpose or because you don't know any better.

    Make the loop end condition be:
    Code:
    a < 60;
    Then add 1 to minutes when the loop finishes (after the loop). At that point, check if minutes == 60, add hours, check hours, etc.

    Of course if a == 60, then a = a - 60 is the same as a = 0;

    This of course, assuming you don't follow Elysia's excellent advice of counting time in seconds (or milliseconds or minutes, depending on the precision you actually need).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  5. using clock()
    By sl4nted in forum C Programming
    Replies: 8
    Last Post: 11-09-2006, 07:16 PM