Home > OS >  For some reason, 48 is being added to this variable?
For some reason, 48 is being added to this variable?

Time:11-15

For some reason when I input the variable 'move' as e.g. "a1" or "c2", move[1] is printed as 1 but when used in arithmetic, is actually equal to 48 move[1]?? I have no idea where this 48 comes from either as literally the highest value in the rest of the code is a 3 as I'm just poorly making tic tac toe since I started c yesterday. Any help much appreciated, thanks!

std::string alphabet = "ABC";
std::vector<std::vector<int>> spaces{{1, 2, 3},{1, 2, 3},{1, 2, 3}};
std::vector<std::vector<int>> taken {{0, 0, 0},{0, 0, 0},{0, 0, 0}};
std::vector<char> symbols {'-','X','O'};
bool ValidateMove(std::string move, int player)
{
    std::cout << move[1] << " " << move[1]   0 << "\n\n";
    int a;
    switch(toupper(move[0]))
    {
        case 'A':
            a = 0;
            break;
        case 'B':
            a = 1;
            break;
        case 'C':
            a = 2;
            break;
    }

    for (int i = 0; i < spaces[a].size(); i  ) // FOR SOME REASON MOVE[1] = 48   i!!!
    {

        /* <debugging prints> */

        std::cout << "\nspaces[a][i]:" << spaces[a][i] << "    move[1]:" << move[1] << "\n"; // outputs as expected
        if (spaces[a][i] > move[1]){
            std::cout << "greater";
        } // shouldn't work and doesn't
        if (spaces[a][i] < move[1]){
            std::cout << "less than";
            std::cout << "\n -= " << move[1]- 0;
        } //SHOULDN'T WORK BUT DOES -> move[1] - 0 is 48   move[1] but when you just std::cout << move[1] you get what you should?

        /* </debugging prints> */

        if (spaces[a][i] == move[1])
        {
            if (player == 1)
            {
                taken[a][i] == 1;
            }
            else
            {
                taken[a][i] == 2;
            }
            for (int f = 0; f < taken.size(); f  )
            {
                for (int g = 0; g < taken[f].size(); g  )
                {
                    std::cout << taken[f][g] << " ";
                }
                std::cout << "\n";
            }

            spaces[a].erase(spaces[i].begin()   i);
            return true;
        }
    }
    return false;
}

CodePudding user response:

C is a language that started out as an extension of C and gradually added higher level features. While there are a few exceoptions, most stuff that works in C works in the same way in C . The result is that even when you are trying to write "modern high level C " the low level characteristics inherited from C sometimes rear their ugly head.

At a low level computers must represent everything as numbers. On nearly all computers you encounter today the numbers 0 to 127 represent the vodes from ASCII*.

Some langauges make a strong distiction between a character and the code number representing that character. C and hence C do not.

When you print a char with cout it is treated as a character, but when you use it in arithmetic it is treated as a number. 48 is the ASCII code for the character '0' 49 is the ASCII code for '1' and so-on.

* Handling of non-ASCII characters is outside the scope of this post.

CodePudding user response:

the character value of 0 is 48 in ascii, you trying to add numbers to character values. change or cast std:string to an appropriate type like int and it will work

  •  Tags:  
  • c
  • Related