C++ input conversion to lowercase for ignoring words written in upper case

422 views Asked by At

I wanted to make a sample code that asks the user for the "secret" code. The user has 5 tries to type in the correct (secret) phrase, if the user fails, the program closes itself. I have done everything, I just have one problem. If the user types in the secret phrase in uppercase letters, the program interprets it as wrong. I have seen some conversion methods but none have worked. Here is the code I have at the moment. I only need it to convert the input to lowercase:

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;
    }

  }


}
1

There are 1 answers

0
Krishnakant Mishra On

Your best approach will be to use tolower() method. In C++, a locale-specific template version of this function (tolower) exists in header .