### Hex to Dec converter

I've written this program which repeatedly offers the choice to convert unsigned Hex to Dec or unsigned Dec to Hex (Teacher wants us do not use the scanf and printf methods with the descriptors %d and %x).
I don't know why it asks to enter hex in either choices?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````#include #include #include using namespace std; int hexToDec(const string &hex); int hexCharToDec(char ch); void DecToHex(int decNum); int main() { cout << "Select 1 for Hex to Dec or 2 for Dec to Hex: "; int x = 0; cin >> x; if( x = 1 ){ cout << "Enter hex number: "; string hex; cin >> hex; unsigned decimal = -1; decimal = hexToDec(hex); if (decimal != -1) cout << "The decimal is " << decimal << endl; } if (x=2){ unsigned decNum = 0; cout << "Enter Decimal Number: "; cin >> decNum; cin.clear(); cin.sync(); cout << "Hexadecimal N.: "; if( decNum )DecToHex(decNum); else cout << "0"; cout << endl << endl; } cin.get(); cin.get(); return 0; } int hexToDec(const string &hex) { int decimalValue = 0; for (int index = 0; index < hex.size(); index++) { try { char ch = toupper(hex[index]); if (!(ch >= 'A' && ch <= 'F' || ch >= '0' && ch <= '9')) { throw runtime_error("is not a hex string"); } else decimalValue = decimalValue * 16 + hexCharToDec(hex[index]); } catch (runtime_error e) { cout << e.what(); return -1; } } return decimalValue; } int hexCharToDec(char ch) { ch = toupper(ch); if (ch >= 'A' && ch <= 'F') return 10 + ch - 'A'; else return ch - '0'; } void DecToHex(int decNum) { int x = decNum; if(decNum / 16 > 0) { x = decNum%16; decNum = decNum /16; DecToHex(decNum); } if( x > 9 ) { switch (x) { case 10 : cout << "A"; break; case 11 : cout << "B"; break; case 12 : cout << "C"; break; case 13 : cout << "D"; break; case 14 : cout << "E"; break; case 15 : cout << "F"; break; } } else { cout << x; } }``````

 I don't know why it asks to enter hex in either choices?

For one simple reason. You're using the single equal sign, which is assigning 1 to x, instead of using the double equal sign, which is to check if a variable is equal to what you are looking for. So, just change the single =, to a ==, and you're done.

 ``1234567891011121314151617181920`` ``````if( x = 1 ){ // Change to a double == cout << "Enter hex number: "; string hex; cin >> hex; unsigned decimal = -1; decimal = hexToDec(hex); if (decimal != -1) cout << "The decimal is " << decimal << endl; } if (x=2){ // Change to a double ==, also unsigned decNum = 0; cout << "Enter Decimal Number: "; cin >> decNum; cin.clear(); cin.sync(); cout << "Hexadecimal N.: "; if( decNum )DecToHex(decNum); else cout << "0"; cout << endl << endl; }``````
Thank you,
Last edited on
how to change the program to convert signed Hex to Dec or signed Dec to Hex?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118`` ``````// Hex Convertor.cpp : main project file. #include #include #include using namespace std; int hexToDec(const string &hex); int hexCharToDec(char ch); void DecToHex(int decNum); int main() { cout << "Select 1 for Hex to Dec or 2 for Dec to Hex: "; int x = 0; cin >> x; if( x == 1 ) { cout << "Enter hex number: "; string hex; cin >> hex; if(hex[0] == '-') { std::string Signed_Hex = hex; unsigned char chTest = std::stoi( Signed_Hex, nullptr, 16 ); char chTest2 = *reinterpret_cast(&chTest); std::cout << Signed_Hex << ": " << static_cast(chTest2) << std::endl; } else { unsigned decimal; decimal = hexToDec(hex); if (decimal != -1) cout << "The decimal is " << decimal << endl; } } if (x==2) { int decNum = 0; cout << "Enter Decimal Number: "; cin >> decNum; cin.clear(); cin.sync(); cout << "Hexadecimal N.: "; if( decNum < 0 ) cout << "-"; DecToHex(decNum); cout << endl << endl; } cin.get(); cin.get(); return 0; } int hexToDec(const string &hex) { int decimalValue = 0; for (int index = 0; index < hex.size(); index++) { try { char ch = toupper(hex[index]); if (!(ch >= 'A' && ch <= 'F' || ch >= '0' && ch <= '9')) { throw runtime_error("is not a hex string"); } else decimalValue = decimalValue * 16 + hexCharToDec(hex[index]); } catch (runtime_error e) { cout << e.what(); return -1; } } return decimalValue; } int hexCharToDec(char ch) { ch = toupper(ch); if (ch >= 'A' && ch <= 'F') return 10 + ch - 'A'; else return ch - '0'; } void DecToHex(int decNum) { decNum = abs(decNum); int x = decNum; if(decNum / 16 > 0) { x = decNum%16; decNum = decNum /16; DecToHex(decNum); } if( x > 9 ) { switch (x) { case 10 : cout << "A"; break; case 11 : cout << "B"; break; case 12 : cout << "C"; break; case 13 : cout << "D"; break; case 14 : cout << "E"; break; case 15 : cout << "F"; break; } } else { cout << x; } }``````