Oct 23, 2012 at 12:54am Oct 23, 2012 at 12:54am UTC
Try putting each conversion into its own function, and instead of using a
void
function, make it return an
int
.
There are a number of different ways to give the user a choice. For example, you could use a string, or maybe an int.
eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
int choice;
cout << "What would you like to convert to?" << endl;
cout << "1-Binary" << endl
<< "2-Hex" << endl
<< "3-Octal" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << binary(choice) << endl;
break ;
case 2:
cout << hex(choice) << endl;
break ;
case 3:
cout << octal(choice) << endl;
break ;
}
//or alternatively...
if (choice == 1)
cout << binary(n) << endl;
else if (choice == 2)
cout << hex(n) << endl;
else if (choice == 3)
cout << octal(n) << endl;
Using a string would allow the user to type what they want:
1 2 3 4 5 6 7 8 9 10 11 12
string choice;
cout << "What would you like to convert to?" << endl;
cin >> choice;
if (choice == "binary" )
cout << binary(n) << endl;
else if (choice == "hex" || choice == "hexadecimal" )
cout << hex(n) << endl;
else if (choice == "octal" )
cout << octal(n) << endl;
The string option could also be done in a switch statement, as with an int: Wrong!
Last edited on Oct 23, 2012 at 3:09pm Oct 23, 2012 at 3:09pm UTC
Oct 23, 2012 at 1:22am Oct 23, 2012 at 1:22am UTC
Sadly, you can't switch on strings in C++, so you'll have to pass on that. The other ways should work, though.
Oct 23, 2012 at 3:13am Oct 23, 2012 at 3:13am UTC
yea i figured that I should make hex and octal functions, I went with this route because i couldn't get my original functions to work. I'll try what you said, thankyou
Oct 23, 2012 at 3:08pm Oct 23, 2012 at 3:08pm UTC
Aah yes, Moeljbcp, my mistake.