简体   繁体   English

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

[英]the isupper(), islower(), toupper(), tolower() functions not working in c++

I have a code fragment something like this: 我有这样的代码片段:

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'));

but it does not converts the Lower alphabets to Upper alphabets... I don't know why... The OS I am using is Windows 7 and the Compiler is Visual C++(please note that I have tested this code in other compilers to but the same problem)... 但是它不会将低位字母转换为高位字母...我不知道为什么...我使用的操作系统是Windows 7,而编译器是Visual C ++(请注意,我已经在其他编译器中测试了此代码,但同样的问题)...

You should use the returned value, toupper takes a character by value (not reference) and returns the upper case result: 您应该使用返回的值, toupper按值(而不是引用)接受一个字符并返回大写结果:

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

Also, the condition should be inverted: 另外,条件应取反:

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

Use this code, toupper itself checks if the character is lower case or not: 使用此代码, toupper本身会检查字符是否为小写:

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

This line of code 这行代码

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

should be re-write as below, 应该如下重写

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

The function, 功能,

int toupper ( int c );

Return Value The uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. 返回值如果存在c,则大写等于c,否则等于c(不变)。 The value is returned as an int value that can be implicitly casted to char. 该值以int值形式返回,该值可以隐式转换为char。

toupper up

islower and isupper tells whether character is upper case or lower case or not. islowerisupper字符是大写还是小写。

toupper or tolower does not convert. touppertolower不会转换。 It takes int parameter and returns an int which is converted character. 它接受int参数,并返回一个int ,它是转换后的字符。

To convert use the following: 要进行转换,请使用以下命令:

  choice = toupper(choice);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM