[英]the isupper(), islower(), toupper(), tolower() functions not working in 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);
这行代码
if(islower(choice) == 0){ toupper(choice); }
应该如下重写
if(islower(choice)){ choice = toupper(choice); }
功能,
int toupper ( int c );
返回值如果存在c,则大写等于c,否则等于c(不变)。 该值以int值形式返回,该值可以隐式转换为char。
islower
和isupper
字符是大写还是小写。
toupper
或tolower
不会转换。 它接受int
参数,并返回一个int
,它是转换后的字符。
要进行转换,请使用以下命令:
choice = toupper(choice);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.