2015-06-18 70 views
0

我想製作一個示例代碼,要求用戶輸入「祕密」代碼。用戶有5次嘗試輸入正確的(祕密)短語,如果用戶失敗,程序自行關閉。我做了所有事情,我只有一個問題。如果用戶用大寫字母輸入密碼,程序會將其解釋爲錯誤。我看到了一些轉換方法,但都沒有工作。這是我目前的代碼。我只需要它將輸入轉換爲小寫:C++輸入轉換爲小寫字母以忽略用大寫字母寫的字

int nextReveal(){ 
std::string code; 
std::string answer = "sample"; 
for (int i = 1; i <= 5; i++){ 

    //std::cout << " " << std::endl; 
    std::cout << "Type in the secret password" << std::endl; 
    std::cin >> code; 
    int tries = 5 - i; 
    if (code != answer && code != answer2 && code != answer3){ 
     std::cout << "You have " << tries << " tries left before program self-destructs" << std::endl; 
     if (tries == 0){ 
      std::cout << " You have used up all your tries. Program is closing" << std::endl; 
      return 1; 

     } 

    } 

    else if (code == answer){ 
     std::cout << "Unlocked" << std::endl; 
     //i = 6; 
     return 0; 
    } 

    } 


} 
+0

也[這裏](https://stackoverflow.com/questions/23943728/case-insensitive-standard-string-comparison-in-c)和[這裏](https://stackoverflow.com/questions/9182912 /不區分大小寫-字符串比較-c)的 – CoryKramer

回答

0

您最好的方法是使用tolower()方法。在C++中,此函數(tolower)的特定於語言環境的模板版本存在於標題中。

相關問題