簡體   English   中英

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

[英]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。

up

islowerisupper字符是大寫還是小寫。

touppertolower不會轉換。 它接受int參數,並返回一個int ,它是轉換后的字符。

要進行轉換,請使用以下命令:

  choice = toupper(choice);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM