# Binary Clock

• 11-12-2006
cerin
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; }```
• 11-12-2006
Salem
> 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
• 11-12-2006
grumpy
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.
• 11-12-2006
cerin
Really sorry. It's been a while and programming in VB surely doesn't help.
• 11-12-2006
Salem
Arghhhhh! Mind Rot!
• 11-13-2006
jafet
Quote:

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.
• 11-13-2006
manutd