The classic casting a char to an int problem

Hi so I'm trying to cast/convert a char that I know will always be a number to an int. I'm given a vector of chars, something like charList[2, *, 5] and I pass those values to this function as argValue1 = '2', argValue2 = '5', and argOpperator = '*'. The if statements catch the opperator just fine but the argValue1 * argValue2 comes out to be something like 'Z'.

I can't change the vector to hold something other then char. I'm given the vector but I could convert them before passing them to this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int infixCalculator::evaluate(char argValue1, char argValue2, char argOpperator){


	if(argOpperator == '+'){
		return (int(argValue1) + int(argValue2));
	}
	if(argOpperator == '-'){
		return (int(argValue1) - int(argValue2));
	}
	if(argOpperator == '*'){
		return (int(argValue1) * int(argValue2));
	}
	if(argOpperator == '/'){
		return (int(argValue1) / int(argValue2));
	}

	return 0;
}


Thanks for any help I can get
When you cast a char to an int like that you get the ASCII value of the number, which isn't equal to the number itself. Often you can just simply do int(some_char - '0') to get the number in the character.
You can also use the atoi function from the standard library to convert a number represented as text (like in a char) to an actual number value.

Here is the documentation of the atoi function:
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
Topic archived. No new replies allowed.