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