Problem with changing character via multiplying

Why is it that everything works with characters except multiplication (well multiplication works if you divide first...but that's not what I want)
here is the code example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{
    char ch = 'B';
    std::cout << "Int of \'B\' " << (int) ch << " Char of \'B\' " << (char) ch << std::endl; //correct
    ch *= 2;
    std::cout << "Int of \'B\' * 2 " << (int) ch << " Char of \'B\' * 2 " << (char) ch << std::endl; // error with int
    ch = 'B' * 2;
    std::cout << "Int of \'B\' * 2 " << (int) ch << " Char of \'B\' * 2 " << (char) ch << std::endl; // error with int
    ch = 'B';
    std::cout << "Int of \'B\' * 2 " << (int) ch * 2 << " Char of \'B\' * 2 " << char((int)ch * 2) << std::endl; /*correct but does not do what I want (edit the actual
    character)*/
    std::cout << "Int of \'B\' * 2 " << (int) 'B' * 2 << " Char of \'B\' * 2 " << char((int)'B' * 2) << std::endl; /*correct but does not do what I want (edit the actual
    character)*/
    ch = 'B' * 2;
    ch /= 2;
    std::cout << "Int of \'B\' *2 / 2' " << (int) ch << " Char of \'B\' * 2 / 2 " << (char) ch << std::endl; /*error with int because of the multiplication should be 66
    and char should be \'B\'*/
    ch = 'B';
    ch /= 2;
    std::cout << "Int of \'B\' / 2 " << (int) ch << " Char of \'B\' / 2 " << (char) ch << std::endl; //correct
    ch *= 2;
    std::cout << "Int of \'B\' / 2 * 2 " << (int) ch << " Char of \'B\' / 2 * 2 " << (char) ch << std::endl; //correct both multiplication and division work here
    ch = 'B';
    std::cout << "Int of \'B\' " << (int) ch << " Char of \'B\' " << (char) ch << std::endl; //correct
    ch += 2;
    std::cout << "Int of \'B\' + 2 " << (int) ch << " Char of \'B\' + 2 " << (char) ch << std::endl; //correct
    ch -= 2;
    std::cout << "Int of \'B\' + 2 - 2 " << (int) ch << " Char of \'B\' + 2 - 2 " << (char) ch << std::endl; //correct
    ch = 'B';
    ch -= 2;
    std::cout << "Int of \'B\' - 2 " << (int) ch << " Char of \'B\' - 2 " << (char) ch << std::endl; //correct
    ch += 2;
    std::cout << "Int of \'B\' - 2 + 2 " << (int) ch << " Char of \'B\' - 2 + 2 " << (char) ch << std::endl; //correct
    ch = 'B';
    std::cout << "Int of \'B\' " << (int) ch << " Char of \'B\' " << (char) ch << std::endl; //corect
}


output is this:

Int of 'B' 66 Char of 'B' B
Int of 'B' * 2 -124 Char of 'B' * 2 ä
Int of 'B' * 2 -124 Char of 'B' * 2 ä
Int of 'B' * 2 132 Char of 'B' * 2 ä
Int of 'B' * 2 132 Char of 'B' * 2 ä
Int of 'B' *2 / 2' -62 Char of 'B' * 2 / 2 ┬
Int of 'B' / 2 33 Char of 'B' / 2 !
Int of 'B' / 2 * 2 66 Char of 'B' / 2 * 2 B
Int of 'B' 66 Char of 'B' B
Int of 'B' + 2 68 Char of 'B' + 2 D
Int of 'B' + 2 - 2 66 Char of 'B' + 2 - 2 B
Int of 'B' - 2 64 Char of 'B' - 2 @
Int of 'B' - 2 + 2 66 Char of 'B' - 2 + 2 B
Int of 'B' 66 Char of 'B' B
Press <RETURN> to close this window...
1
2
std::cout << static_cast<int>(std::numeric_limits<char>::min()) << std::endl;
std::cout << static_cast<int>(std::numeric_limits<char>::max()) << std::endl;
-128
127
Shouldn't it be -128 then and not -124
I guess I can try and make an object that will contain all of the unicode characters then use that instead of a character. I'll let you know how it goes.
Last edited on
Topic archived. No new replies allowed.