alright so i'm making a program now that takes a decimal number from the user and outputs the number out again but counting in binary, octal, and hexadecimal. I can get binary and octal easily but hexadecimal is hard i don't know how to do it.

any help?

2. Code:
```switch (num % 16) {
case 10:
hex = 'A'
break;
case 11:
hex = 'B'
break;
case 12:
hex = 'C'
break;
case 13:
hex = 'D'
break;
case 14:
hex = 'E'
break;
case 15:
hex = 'F'
break;
}```

3. ok considering the fact that i'm a noob to this, can you tell me how i would apply that switch statement to what i'm trying to do? lol sorry.

4. Post what you tried.

5. ## asfase

i haven't really tried anything, i put that code into my program and i don't really know how to code around it so that the user can input 9999 and get the hex number.

6. > I can get binary and octal easily but hexadecimal is hard
I thought this implied you had some code.

7. This is what i have so far. I know it's way more complicated than it has to be and i know i use gotos all over the place but bear with me, i'm a noob.

Code:
```#include <iostream.h>

main()
{
int choice1, remainder1, remainder2, remainder3, remainder4, remainder5, remainder6, remainder7, remainder8, remainder9, remainder10, remainder11, remainder12, remainder13, remainder14;
int base1 = 2;
int base2 = 8;
int value, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14;
char choice2, hex;

A:	cout << "Welcome to the number converter." << endl;
D:	cout << "Enter a number between 0 and 9999. " << endl;
cin >> value;

if ( value > 9999 )
{
cout << "Please enter a number between 0 and 9999." << endl;
cin.clear();
cin.ignore();
cout << endl;
goto D;
}
else if ( value < 0 )
{
cout << "That is not a positive number.  Please enter a positive number." << endl;
cin.clear();
cin.ignore();
cout << endl;
goto D;
}
else if ( value > 0 && value < 9999 )
{
goto E;
}
else
{
cout << "Please enter a number Por Favor!" << endl;
cin.clear();
cin.ignore();
cout << endl;
goto D;
}

E:	cout << "Now, choose what base you would like to convert to." << endl;
cout << "   1.) Binary" << endl;
cout << "   2.) Octal" << endl;
cout << "   3.) Hexadecimal" << endl;
cout << "Please enter your choice: ";
cin >> choice1;

if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Don't enter a letter stupid." << endl;
goto E;
}

if (choice1 > 3)
{
cin.clear();
cin.ignore();
cout << endl << "C'mon idiot it gives you the choices now obey!" << endl;
goto E;
}

if (choice1 < 1)
{
cin.clear();
cin.ignore();
cout << endl << "C'mon idiot it gives you the choices now obey!" << endl;
goto E;
}

switch ( choice1 )
{
case 1:
value1 = value / base1;
remainder1 = value % 2;
value2 = value1 / base1;
remainder2 = value1 % 2;
value3 = value2 / base1;
remainder3 = value2 % 2;
value4 = value3 / base1;
remainder4 = value3 % 2;
value5 = value4 / base1;
remainder5 = value4 % 2;
value6 = value5 / base1;
remainder6 = value5 % 2;
value7 = value6 / base1;
remainder7 = value6 % 2;
value8 = value7 / base1;
remainder8 = value7 % 2;
value9 = value8 / base1;
remainder9 = value8 % 2;
value10 = value9 / base1;
remainder10 = value9 % 2;
value11 = value10 / base1;
remainder11 = value10 % 2;
value12 = value11 / base1;
remainder12 = value11 % 2;
value13 = value12 / base1;
remainder13 = value12 % 2;
value14 = value13 / base1;
remainder14 = value13 % 2;
cout << remainder14 << remainder13 << remainder12 << remainder11 << remainder10 << remainder9 << remainder8 << remainder7;
cout << remainder6 << remainder5 << remainder4 << remainder3 << remainder2 << remainder1;
cout << endl << endl << "Exclude the zeros to the left of the first 1 digit." << endl;
break;
case 2:
value1 = value / base2;
remainder1 = value % 8;
value2 = value1 / base2;
remainder2 = value1 % 8;
value3 = value2 / base2;
remainder3 = value2 % 8;
value4 = value3 / base2;
remainder4 = value3 % 8;
value5 = value4 / base2;
remainder5 = value4 % 8;
value6 = value5 / base2;
remainder6 = value5 % 8;
value7 = value6 / base2;
remainder7 = value6 % 8;
value8 = value7 / base2;
remainder8 = value7 % 8;
value9 = value8 / base2;
remainder9 = value8 % 8;
value10 = value9 / base2;
remainder10 = value9 % 8;
value11 = value10 / base2;
remainder11 = value10 % 8;
value12 = value11 / base2;
remainder12 = value11 % 8;
value13 = value12 / base2;
remainder13 = value12 % 8;
cout << remainder13 << remainder12 << remainder11 << remainder10 << remainder9 << remainder8 << remainder7;
cout << remainder6 << remainder5 << remainder4 << remainder3 << remainder2 << remainder1;
cout << endl << endl << "Exclude the zeros to the left of the first whole number digit." << endl;
break;
case 3:
break;

C:	cout << endl << "Would you like to try again? y or n ";
cin >> choice2;

if ( choice2 == 'y' )
{
cin.clear();
cin.ignore();
cout << endl << endl;
goto A;
}

if ( choice2 == 'n' )
{
cout << endl << endl;
goto B;
}

if ( choice2 != 'n'||'y' )
{
cout << "That ain't right.  try again." << endl;
cin.clear();
cout << endl << endl;
goto C;
}

B:  cout << "Thanks for.....playing?" << endl;

return 0;
}```

8. This is fun:
Code:
`// For class, eh? Removed.`
Don't worry about the string stream if you don't know it. You can do this with plain ol' strings as well.

9. this is also something i don't understand.

LOL i feel dwarfed by you guys.

10. Ah... don't feel dwarfed by me. Salem, yes. Me, no.

11. grrrr with hexadecimal.

this is for a c++ class btw and this is due like....tomorrow.

crap XD

12. I hope you don't expect to get anything better than an F on that program.

13. You should really consider using a loop since you can really see an iterative structure in that code.
Code:
```while(Number > 0)
{
Digit = Number % Base;
PrependToResult(Digit);
Number /= Base;
}```
To convert the number to a hex character:
Code:
```char NumberToHex(int Number)
{
return (Number < 10) ? ('0' + Number) : ('A' + Number - 10);
}```

14. Well, for oct and hex are you allowed to do this:
Code:
```int main()
{
int number;
cout << "Enter a number: ";
cin >> number;

cout << "Oct: " << oct << number << endl;
cout << "Hex: " << hex << number << endl;

return 0;
}```

15. I nominate hk_mp5pdw the king of standard C++ keywords and functions.

...and no, I don't think he can do that one.

Popular pages Recent additions