我有一个类似这样的代码片段:
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/