Binary Clock

This is a discussion on Binary Clock within the C++ Programming forums, part of the General Programming Boards category; I made a binary clock for school in Visual Basic which I had to use. Then I decided to remake ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Binary Clock

    I made a binary clock for school in Visual Basic which I had to use. Then I decided to remake it for fun in C++. It doesn't calculate the binary numbers correctly for some reason. I know it doesn't change the increment the units of time yet because that was a problematic bit of code, but you can still see it in there although commented out.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <time.h>
    
    
    
    using namespace std;
    
    
    void wait ( long milliseconds )
    {
    	long timeout = clock() + milliseconds;
    	while ( clock() < timeout ) continue;
    }
    
    int main()
    {
    int hours,hours2;
    int minutes, minutes2;
    int seconds,seconds2;
    int clock[3][6];        //array for display
    
     cout<<"What are the hours (military time):";    //get input for start position of clock
     cin>>hours2;  
     cout<<"\nMinutes:";
     cin>>minutes2;
     cout<<"Seconds:";
     cin>>seconds2;
     
     clock[0][0]=0;  // this position will never be a 1 because there are only 24 hours in a day
    while (true){
          
          seconds=seconds2;   //returns seconds,minutes, and hours to original values for recalculation
          minutes=minutes2;
          hours=hours2;
          
    
    /*if (hours2=24){ this code is also problematic but has been commented out in an attempt to solve one problem at a time
       hours2=0;
    }
    if (minutes2=60){
       minutes2=0;
       hours2+1;
    }
    if (seconds2=60){
       seconds2=0;
       minutes2+1;
    }*/
    
    wait(1000);        //wait one second
          
    if (hours-16>-1) {         //figure out the 1's and 0's and put them in an array
       clock[0][1]=1;
       hours-16;
    }
       else{
       clock[0][1]=0;
    }
    
    if (hours-8>-1){
       clock[0][2]=1;
       hours-8;
    }
       else{
       clock[0][2]=0;
    }
    
    if (hours-4>-1){
       clock[0][3]=1;
       hours-4;
    }
       else{
       clock[0][3]=0;
    }
    if (hours-2>-1){
       clock[0][4]=1;
       hours-2;
    }
       else{
       clock[0][4]=0;
    }
    
    if (hours-1>-1){
       clock[0][5]=1;
       hours-1;
    }
       else{
       clock[0][5]=0;
    }
    
    if (minutes-32>-1){
       clock[1][0]=1;
       minutes-32;
    }
       else{
       clock[1][0]=0;
    }
    
    
    if (minutes-16>-1){
       clock[1][1]=1;
       minutes-16;
    }
       else{
       clock[1][1]=0;
    }
    
    if (minutes-8>-1){
       clock[1][2]=1;
       minutes-8;
    }
       else{
       clock[1][2]=0;
    }
    
    if (minutes-4>-1){
       clock[1][3]=1;
       minutes-4;
    }
       else{
       clock[1][3]=0;
    }
    
    if (minutes-2>-1){
       clock[1][4]=1;
       minutes-2;
    }
       else{
       clock[1][4]=0;
    }
    
    if (minutes-1>-1){
       clock[1][5]=1;
       minutes-1;
    }
       else{
       clock[1][5]=0;
    }
    
    if (seconds-32>-1){
       clock[2][0]=1;
       seconds-32;
    }
       else{
       clock[2][0]=0;
    }
    
    if (seconds-16>-1){
       clock[2][1]=1;
       seconds-16;
    }
       else{
       clock[2][1]=0;
    }
    
    if (seconds-8>-1){
       clock[2][2]=1;
       seconds-8;
    }
       else{
       clock[2][2]=0;
    }
    
    if (seconds-4>-1){
       clock[2][3]=1;
       seconds-4;
    }
       else{
       clock[2][3]=0;
    }
    
    if (seconds-2>-1){
       clock[2][4]=1;
       seconds-2;
    }
       else{
       clock[2][4]=0;
    }
    if (seconds-1>-1){
       clock[2][5]=1;
       seconds-32;
    }
       else{
       clock[2][5]=0;
    }
    
    cout<<"    Hours  Minutes  Seconds";        //recursively output the array of 1's and 0's
    for (int row=0; row<6;row++){
        cout<<"\n";
    for(int column=0;column<3;column++){
    
        cout<<"      "<<clock[column][row];
    }
    }
    cout<<"\n";
    }
      cin.get();
      cin.ignore();
      return 0;
    }
    My computer is awesome.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > hours-16
    1. Why don't you use a couple of loops to extract binary values - or better yet, write a function.
    2. A good compiler would have reported a warning along the lines of expression has no effect.
    Try
    hours -= 16
    if you really meant
    hours = hours-16
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,628
    You have mixed up usage of operators in a few places. For example, in your code;
    Code:
    if (minutes2=60){
       minutes2=0;
       hours2+1;
    }
    Testing for equality is done with two equals signs (eg if (minutes == 60)). Using the single equals sign assigns minutes2 to 60, and because the result of that operation is always non-zero, the condition in the if statement is always true.

    In this example, the line "hours2+1" computes the value of hours2+1 but does nothing with the result (and most aggressive optimisers will simply throw that line away). I'm assuming you wanted this line to be "hours2 = hours2 + 1" or (more simply) "++hours2".

    As salem said, cranking up the levels of warnings given by your compiler will give result in some warnings from the compiler. In practice, it is a good idea in C++ (or C) to tweak your compiler options so it warns about everything it can, and attempt to write your code to stop the compiler complaining.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Really sorry. It's been a while and programming in VB surely doesn't help.
    My computer is awesome.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Arghhhhh! Mind Rot!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    and programming in VB surely doesn't help.
    Understatement of the Year.

    You're probably using Dev-C++ as well. Do us all a favor:

    Tools > Compiler Options > Check "Add following compiler commands" and add "-W -ansi -pedantic" to the text box. This will give you lots of warnings you may want to ignore, but will also give you many you probably won't.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Good advice, jafet.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Binary Clock
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-13-2003, 10:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21