## Recommended Posts

gdc4lunch    122
hey gamedev.net! i have recently started programming c++ and stumbled across this site. the reason im posting here today is because i need help with one of my projects. here is how it all begann: a friend of mine at school recently bought one of these binary watches, Binary watch you know, the ones where you have to see which LEDs are on and then add the numbers according to thier position, for example 8 x(4) x(2) 1 would be 9 o'clock and the minutes are calculated by adding 32, 16, 8, 4, 2 and 1... anyway, to cut one short story long, i decided to see if i could not program something like that in c++, at firt trying with 'switch/case' and then moving on to something similar to that which was used on day five of 'Teach Yourself C++ in 21 Days' for the Fibonacci puzzle. (i am on day 6, thats where they start Obj-Oriented programming) the result looks a bit like this...(not quite working and not very elegant(or to tell the truth, horribly ugly and completely useless))
#include <iostream>
#include <stdlib.h>

int minutes(int t_minutes);
int hours(int t_hours);

int main()
{
int t_hours, t_minutes, timelefth, timeleftm;
cout << "what is the time? hours (1-12) and minutes (0-59): ";
cin >> t_hours;
cin >> t_minutes;

cout << t_hours <<":"<< t_minutes << "\n";

timelefth = hours(t_hours);
timeleftm = minutes(t_minutes);

cout << timelefth;
cout << timeleftm;
cout << "\n\n\n\n\n\n";
system("PAUSE");
return 0;
}

int hours (int t_hours)
{
cout << "calculating time in hours \n \t 8\t4\t2\t1\t " << endl;
cout << " \t";

if (t_hours > 7)
{
cout << "8\t";
return (t_hours - 8);
}
else
{
if (t_hours > 3)
{
cout << "4\t";
return (t_hours - 4);
}
else
{
if (t_hours > 1)
{
cout << "2\t";
return (t_hours - 2);
}
else
{
if (t_hours > 0)
{
cout << "1\t";
return (t_hours - 1);
}
else
{
cout << "time's up!";
return (1);
}
}
}
}

}

int minutes (int t_minutes)
{
cout << "\n\ncalculating time in minutes \n \t 32\t 16\t 8\t 4\t 2\t 1\t" << endl;
cout << "\t";

if (t_minutes > 31)
{
cout << "32\t";
return (t_minutes - 32);
}
else
{
if (t_minutes > 15)
{
cout << "16\t";
return (t_minutes - 16);
}
else
{
if (t_minutes > 7)
{
cout << "8\t";
return (t_minutes -8);
}
else
{
if (t_minutes > 3)
{
cout << "4\t";
return (t_minutes -4);
}
else
{
if (t_minutes > 1)
{
cout << "2";
return (t_minutes -2);
}
else
{
cout << "times up!";
return (1);
}
}
}
}
}

}


any help would really be appreciated, i have no clue what is wrong! thanks in advance gdc4lunch

##### Share on other sites
Antheus    2409
Quote:
 any help would really be appreciated, i have no clue what is wrong!thanks in advance gdc4lunch

Well, neither do I, since the code doesn't do anything related to binary clock.

So what are you trying to do. Decode the time displayed by binary clock? Simulate a binary clock? Encode current time as binary?

Here's the trick: All data stored in memory is already in binary.

So all that remains is to display it.

void binary_print( unsigned int value, int n_bits ){	int index = 6;        // Display maximum of 6 digits	if (n_bits > 6) n_bits = 6;                // If less than 6 digits, pad with spaces	while ( index > n_bits ) {		std::cout << " ";		index--;	}        // Iterate through the number, displaying # for 1, _ for 0	while ( n_bits ) 	{                // Check if bit is set		if ( value & ( 1 << ( n_bits - 1 ) ) ) {			std::cout << "#";		} else {			std::cout << "_";		}		n_bits--;	}	std::cout << std::endl;}binary_print( hours, 4 );binary_print( minutes, 6 );binary_print( seconds, 6 );

Calling this with:
binary_print( 10, 4 );binary_print( 37, 6 );binary_print( 56, 6 );

will produce:
  #_#_#__#_####___

##### Share on other sites
gdc4lunch    122
thanks for your reply, and yes: the general idea was to simulate a binary clock, just for some practise. my next step was going to be figuring out how to use the computers internal clock instead of having to enter the time at the beginning. i'm afraid i havent quite figured it all out yet, the last 3 lines for example

( binary_print( hours, 4 ); binary_print( minutes, 6 ); binary_print( seconds, 6 ); )

are difficult for me to understand, please keep in mind that i have been programming for only a very short period of time, hence my expierience is very limited

##### Share on other sites
Ezbez    1164
binary_print is a function that he defined at the top of the file. At the bottom he is calling it three times, each with different parameters. If you haven't yet learned functions, then perhaps you should wait a bit longer. They are pretty fundamental. However, you can still understand his code. When the function is called, it runs the part of code between the brackets. So when binary_print is called, it runs:

void binary_print( unsigned int value, int n_bits )
{

///Everything between these brackets is run when binary_print is called!

}

So he is running that same bit of code three times. However, each time you run a function, it takes in parameters (also called arguments). Those are the two variables at the top of the function: value and n_bits. Noticed how they are defined in between the parenthesis after the function name "binary_print". When he calls binary print he 'fills in' those two variables with two numbers:

binary_print(42,1337); // for example

That line of code would run the code between binary_prints brackets with the variables value and n_bits set to 42 and 1337, respectively. Note that those are arbitrary numbers that you shouldn't use.

So, those three lines runs the code between the brackets three times, each time with different values for value and n_bits. Does that clear things up? It has nothing to do with actually reading the value from the internal clock on the computer - he just made a way for you to display that once you get it.

##### Share on other sites
Antheus    2409
The hours, minutes and seconds are variables that hold the respective values.

They are just an example of how to call the binary_print, not part of the function.

The example is in the post.

Second parameter specifies how many bits do you want to display (4 for hours, 6 for minutes and seconds)