Trying to convert Hex to Decimal. Having some trouble. I think there is something wrong with my formula at the bottom of hex2dec.cpp or the for statement located there. Thanks in advance for any advice.
Yes, the for loop at the end goes on forever, since i is increasing and will always be >= 0. Should be for (--i; i>= 0; i--)
Apart from that, there is a problem here:
char hex [10];
int value = 0;
cout << "Enter Hex value: ";
cin >> hex;
hex is declared as an array of chars. However, while reading the input, you are reading only hex, meaning only 1 character. To make it as a string, you need to terminate it with a null character and then pass it, and in hex2dec function, you need to check the size of the string.
An easier approach is to use
string hex;
instead of
char hex [10];
. Then you can use cin>>hex directly.
Things you will need to change in that case:
1. Declaration of hex - string hex;
2. Function parameter in header file - int hex2dec(string hexValue);
Include the string header file here.
3. for loop in cpp file - for (i = 0; i < hexValue.size(); i++)