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

标签 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++(请注意,我已经在其他编译器,但同样的问题)...

最佳答案

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

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

此外,条件应该反转:

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

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

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

关于c++ - isupper()、islower()、toupper()、tolower() 函数在 C++ 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042792/

相关文章:

c++ - 如何增量编码 C/C++ 结构以通过套接字传输

c++ - string.find 函数返回奇数

c++ - 无法在 gdb 中设置断点

c# - 如何使用 SWIG 生成的接口(interface)在 C# 中正确向下转换?

c++ - 使用 placement new 作为复制赋值运算符不好吗?

c++ - 写入文件权限被拒绝

c++ - 如何使用 OpenCV 降低图像的每像素位数

c++ - 二维数组值 C++

c++ - 如何将一个 std::vector move 附加到另一个?

python - 将带有子元素的整个 QGraphicSscene 保存到文件中