Lets say i initialize a variable x and give it a value of A, and i declare value A to be 10, why wouldn't this work and how would I do something like this?
Almost there , so if I was to input for int x , 10A would the programme recognize that A has a value of 10?
To do what you are asking means that you need to parse the string for alpha characters, then look up the value in a dictionary to determine the value of "A".
You would need to first input the value, in this case 10A, as a string, then "break" the string into two parts, the numeric and the alphabetic. At that point you take the value of the alpha part and do what you need to do with it.
remember that the computer does not know what the idea of "10A" is supposed to be. What the CPU sees is a bunch of on and off switches to determine a truth value. the visible representation of "10A" is only possible through thousands of computations.
Let me ask you this, what do you intend to do with this "10A"?
I was originally working on a function that converts Hexadecimal values to decimal values for a bonus mark on my assignment. So if some one was to input the hexadecimal value of 10A, my code would break up the number into 3 bits, the A part then multiply it by 16^0, 0 part multiply it by 16^1 and finally the 1 part multiplied by 16^2 then add up all those values and return the answer.
to answer your original question then, there is no pre defined type which will take a number and letter combination in c++ and treat it as a number. You can format your output using ios flags to have your number read as hex, but there are no manipulators to do what you are asking.
std::string.length() - 1 will give you the number of characters in our string without the \0 terminating character.
The same code will work for 'B' - 'F'. However, since you would also be extracting the numbers as characters, you need:
1 2 3 4
char ch = /* whatever */
int x;
if (ch >= '0' && ch <= '9') x = ch - '0';
elseif (/* a valid letter */) x = ch - 'A';
The tricky part of this is, if you are using a c-string (and maybe std::string too), is that zero marks the end of the c-string. You would not have success with this:
1 2 3 4 5 6
char arr[2] = {'1', '0', '\0'};
for (/* each letter in arr */)
{
if letter is number, letter -= '0'
}
// 2nd index of arr is now zero, which will mess up any c style function.