Can Char be given numerical values?

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?
char's can be given numerical values, and the character it represents is determined by ASCII value. Is this what you're asking?
Why not redeclare x as 10?
Anyways, as A is a character, you just can't declare it as lvalue
For example I input a value of 10A , and how do i make the computer recognize A as a value of 10?
you are asking about variables. To define a variable named "A" you would this:
 
int A = 10;


whenever you want to access "A" it would hold a value of 10.

Is this what you are asking?
Almost there , so if I was to input for int x , 10A would the programme recognize that A has a value of 10?
if I was to input for int x , 10A would the programme recognize that A has a value of 10?

No. Depending on the context, it might recognise that x has a value of 10. Or more likely, it would give an error, since 10A is not a number.
Last edited on
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.
'A' is output as a character, but it is really a number:
1
2
char ch = 'A';
cout << (int)ch << '\n';  // prints 65 


Therefor, to get the value of A as ten:
1
2
char ch = 'A';
int x = ch - 'A' + 10;  // ch - 'A' = 0, 0 + 10 = 10 


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';
else if (/* 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. 

Last edited on
Topic archived. No new replies allowed.