toupper返回整数而不是char

时间:2014-09-21 19:54:37

标签: c++ toupper

用于以下功能

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;
    }
}

而不是为char board返回chars [8] [8]它返回整数,所以我的输出看起来像

 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 

而不是

的预期输出
 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

我也试过声明一个char a = board [i] [j]; cout&lt;&lt;在toupper(一);试图将变量类型确认为字符并收到相同的输出。

这是一个类的作业,所以我不期待太多的帮助,我只想知道为什么我的函数返回整数代替字符,以便我知道我的错误是为了将来参考,谷歌没有帮助很多。是toupper的某种范围问题吗?

4 个答案:

答案 0 :(得分:2)

toupper的意图是它可以使用除英语之外的其他语言,因此它必须支持大于8位char的输入和输出,因此应返回可以转换的内容成为unicode或UTF字符。

只需将其转换为char,可能会导致错误代码的来源,具体取决于软件的用途。

看看这个关于如何将它用于宽字符和unicode的问题。

Convert a unicode String In C++ To Upper Case

答案 1 :(得分:0)

如果存在这样的值,Toupper函数返回等效于c的大写,否则返回c(不变)。该值作为int值返回,可以隐式地转换为char。

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

答案 2 :(得分:0)

文档清晰:http://www.cplusplus.com/reference/cctype/toupper/

int toupper ( int c );

所以你只需要转向char

cout << " " << (char) toupper(board[i][j]) << " ";

答案 3 :(得分:0)

你需要使用cout << char(toupper(board[i][j]));解决愚蠢的返回类型的toupper。

相关问题