I'm not sure exactly how. This is what I have:
So the functions I wrote are:
1)print_bits_ : converts base-10 to binary
2) is_on : to test if a single bit in target val is on or not
3) turn_on : turn on the bit
4) turn_off : " " off " "
Code:
void dec_to_bin(int argc, char** argv)
{
string menu
(
"Radix-10 to Binary converter v1.0 by monkey_c_monkey\n"
"=========================================\n"
"How to use: (NB: all inputs are optional)\n"
"low [target] [check status] [change bit in target] [print target in binary]\n"
"[TARGET]\n"
"e.g. 2 (any unsigned integer) or '_' if you want to use previous target\n"
"[CHECK STATUS]\n"
"[target] [s] [which bit to shift to for the mask]\n"
"[CHANGE BIT IN TARGET]\n"
"[1] turn on bit: [target] [o] [which bit]\n"
"[2] turn off bit: [target] [f] [which bit]\n"
"[PRINT]\n"
"[target] [p]\n"
"[HELP]\n"
"prints instructions to use program: [h]\n"
);
//the inputs to program
unsigned int target;
char option = ' ';
unsigned int shift = 0;
unsigned int your_target = 0;//in case user forgets to enter it
target = atoi(argv[1]);
option = *argv[2];
shift = atoi(argv[3]);
if ( target != '_' )//we want to continue using the initial target input user entered
your_target = target;
else
your_target = your_target;//keep as is
switch (argc)
{
case 3://[exe] [t]/[_] [p]/[h]
if ( option == 'h' )
cout << menu << endl;
else if ( option == 'p' )
print_bits_(your_target);
else if ( option == 's' )
is_on(your_target, shift);
else if ( option == 'o' )
turn_on(your_target, shift);
else if ( option == 'f' )
turn_off(your_target, shift);
break;
case 4://[exe] [t]/[_] [s]/[o]/[f] [shift]
if ( option == 's' )
is_on(your_target, shift);
else if ( option == 'o' )
turn_on(your_target, shift);
else if ( option == 'f' )
turn_off(your_target, shift);
break;
}
}
int main(int argc, char* argv[])
{
char input = 'n';
string continue = "Do you want to run program again (y\n)?";
if ( argc > 1 )
{
do
{
dec_to_bin(argc, argv);
cout << continue << endl;
}
while ( input == 'y' );
}
return 0;
}
EDIT: it doesn't crash anymore since I just forgot to check condition to make sure: and all functions work fine, but it exits, so how can I get chance to choose input of 'y' to continue?
So when I run first time in cmd.exe (I'm using Windows...):
============================================
C:\Users\joey\Desktop\cpp_workspace>conver_dec_to_ bin 2 p
0000 0000 0000 0000 0000 0000 0000 0010
Do you want to run program again (y/n)?
C:\Users\joey\Desktop\cpp_workspace>
*program exits, so I don't have chance to answer the prompt: "Do you want to run program etc....?"