isupper(),islower(),toupper(),tolower()函数在c ++中不起作用

时间:2013-11-18 08:02:21

标签: c++

我有一个像这样的代码片段:

char choice;
do
{
    cout << "A. Option 1" << endl;
    cout << "B. Option 1" << endl;
    cout << "C. Option 1" << endl;
    cout << "D. Option 1" << endl;
    cout << "Option: ";
    cin >> choice;
    if(islower(choice) == 0){ toupper(choice); } // for converting Lower alphabets to Upper alphabets so as to provide flexibility to the user
}while((choice != 'A') && (choice != 'B') && (choice != 'C') && (choice != 'D'));

但它没有将低位字母转换为高位字母...我不知道为什么......我使用的操作系统是Windows 7,而编译器是Visual C ++(请注意我已经测试了这段代码)其他编译器,但同样的问题)...

3 个答案:

答案 0 :(得分:4)

您应该使用返回的值,toupper按值(不是引用)获取字符并返回大写结果:

choice = toupper(choice);
^^^^^^^^

此外,条件应该反转:

if (islower(choice)) // not: if(islower(choice) == 0)

使用此代码,toupper本身会检查字符是否为小写:

cin >> choice;
choice = toupper(choice);

答案 1 :(得分:2)

这行代码

if(islower(choice) == 0){ toupper(choice); }

应该重写如下,

if(islower(choice)){ choice = toupper(choice); }

功能,

int toupper ( int c );

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

toupper

答案 2 :(得分:1)

islowerisupper判断字符是大写还是小写。

touppertolower未转化。它需要int参数并返回转换后的字符int

转换使用以下内容:

  choice = toupper(choice);