Search code examples
c++toupper

toupper returns integer rather than char


for the following function

void display()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (board[i][j] < 84 && (i+j)%2 == 0)
                SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x70);
            else if (board[i][j] < 84 && (i+j)%2 == 1)
                SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0xc0);
            else if (board[i][j] > 97 && (i+j)%2 == 0)
                SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x7c);
            else if (board[i][j] > 97 && (i+j)%2 == 1)
                SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0xc7);
            cout << " " << toupper(board[i][j]) << " ";
        }
        cout << endl;
    }
}

instead of returning chars for the char board[8][8] it returns integers so my output looks like

 82  78  66  81  75  66  78  82

 80  80  80  80  80  80  80  80 

 32  32  32  32  32  32  32  32 

 32  32  32  32  32  32  32  32 

 32  32  32  32  32  32  32  32 

 32  32  32  32  32  32  32  32 

 80  80  80  80  80  80  80  80 

 82  78  66  81  75  66  78  82 

rather than the expected output of

 R  N  B  Q  K  B  N  R

 P  P  P  P  P  P  P  P




 P  P  P  P  P  P  P  P

 R  N  B  Q  K  B  N  R

I have also tried declaring a char a = board[i][j]; cout << toupper(a); in an attempt to confirm the variable type as a character and received the same output.

this is an assignment for a class so i don't expect much help, i just want to know why my function is returning integers in place of chars so that i know what my mistake is for future reference, Google didn't help much. is it some sort of scope issue with toupper?


Solution

  • Toupper function returns the uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.

    http://www.cplusplus.com/reference/cctype/toupper/