【问题标题】:C++ input conversion to lowercase for ignoring words written in upper case [duplicate]C ++输入转换为小写以忽略以大写形式编写的单词[重复]
【发布时间】:2015-09-04 11:54:49
【问题描述】:

我想制作一个示例代码,向用户询问“秘密”代码。用户有 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;
    }

  }


}

【问题讨论】:

标签: c++ input lowercase


【解决方案1】:

您最好的方法是使用 tolower() 方法。在 C++ 中,此函数的特定于语言环境的模板版本(下)存在于 header 中。

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多