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;
}