Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

c++ - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The intention for toupper is that it can work in other languages than English, and hence it would have to support input and output which is larger than the 8 bit char, and therefor should return something which can be transformed into a unicode or UTF-character.

Simply just casting it to char is probably a source of buggy code for later on depending on what the purpose of your software is.

Have a look at this question on how to use it for wide characters and unicode.

Convert a unicode String In C++ To Upper Case


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...