我想制作一个示例代码,询问用户“秘密”代码。用户有5次尝试键入正确的(秘密)短语,如果用户失败,程序将自行关闭。我做了一切,我只有一个问题。如果用户以大写字母键入密码短语,则程序将其解释为错误。我见过一些转换方法,但都没有。这是我目前的代码。我只需要它将输入转换为小写:
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 :(得分:0)
您最好的方法是使用tolower()方法。在C ++中,标题中存在此函数(tolower)的特定于语言环境的模板版本。