简体   繁体   中英

how to tell if a character is inside a string c++

I am making a hangman game on my own and I have ran into a problem where I need to know if the guess is in the secret word. if it is I will change the bool innum to true, if it is not it will stay false. i have looked it up and cannot find anything that works. also the name printe is just the name of the string its just what I've named it. here is the code I am working with:

using namespace std;
#include <iostream>
#include <conio.h>


void title();

void rightanswer();

void try1();
void try2();
void try3();
void try4();
void try5();
void try6();

void spacer();

int main()
{
    bool innum = false;
    int printe = 0, attempts_wrong = 0, trynum = 6;
    char guess;
    string secretword, hint1, hint2;
    title();
    // the welcoming senteces
    cout << "Welcome to HANG MAN\n\n" << endl;
    cout << "Please enter the secret word (no spaces): ";
    cin >> secretword;

    // the hints
    cout << "\nenter the first hint: ";
    cin >> hint1;


    cout << "\nenter the second hint: ";
    cin >> hint2;

    //explanation for hints
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; //so guesser cant see word

    cout << "\nthe hints will be used as the guesser runs out of attemptts" << endl;
    cout << "the first hint will be used immediately\n\n" << endl;
    cout << "press any button to start...";
    _getch();

    cout << "\n\n" << endl;

    for (int i = 0; secretword[i] != '\0'; ++i)
    {
        printe++;
    }

    rightanswer();
    cout << "\nyour word is " << printe << " letters long" << endl;

    if (attempts_wrong == 0)
    {
        cout << "your first hint is: ";
        cout << hint1 << endl;
    }
    if (attempts_wrong == 3)
    {
        cout << "your second hint is: ";
        cout << hint2 << endl;
    }


   

    cout << "enter a letter: ";
    cin >> guess;

    // im gonna have the code go here
    // <-----------------------------


    if (innum == true)
    {
        spacer();
        cout << guess << " is in the secret word" << endl;
        rightanswer();
    }
    else if (innum == false)
    {
        spacer();
        cout << guess << " is not in the secret word" << endl;
        rightanswer();
        attempts_wrong++;
    }

    return 0;
}



void title() {
    
    cout << "*****************************************" << endl;
    cout << "*                           _____       *" << endl;
    cout << "*   |     |    /\\   |\\   | |            *" << endl;
    cout << "*   |_____|   /__\\  | \\  | |  ___       *" << endl;
    cout << "*   |     |  /    \\ |  \\ | |     |      *" << endl;
    cout << "*   |     | /      \\|   \\| |_____|      *" << endl;
    cout << "*                                       *" << endl;
    cout << "*      |\\     /|    /\\   |\\   |         *" << endl;
    cout << "*      | \\   / |   /__\\  | \\  |         *" << endl;
    cout << "*      |  \\ /  |  /    \\ |  \\ |         *" << endl;
    cout << "*      |   V   | /      \\|   \\|         *" << endl;
    cout << "*                                       *" << endl;
    cout << "*****************************************" << endl;

}


//head, body, 2 arms, 2 legs - 6 in total

void rightanswer() {
    //if the guess is right and the start

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "       |   " << endl;
    cout << "     __|__ " << endl;
}

void try1() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "       |   " << endl;
    cout << "     __|__ " << endl;
}
void try2() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "   |   |   " << endl;
    cout << "     __|__ " << endl;

}
void try3() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|   |   " << endl;
    cout << "     __|__ " << endl;

}
void try4() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|\  |   " << endl;
    cout << "     __|__ " << endl;

}
void try5() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|\  |   " << endl;
    cout << "  /  __|__ " << endl;

}
void try6() {

    cout << "    ___    " << endl;
    cout << "   |   |   " << endl;
    cout << "   O   |   " << endl;
    cout << "  /|\  |   " << endl;
    cout << "  / \__|__ " << endl;

    cout << " YOU LOSE" << endl;
    cout << "you have run out of guesses";
    exit(0);

}

// it creates a line to differ one try from the other
void spacer() {
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}

I don't know how much to show so that is all of it, its probably not the best but it works. I know that I don't some of the voids functions at the bottom but it works for now. if you have any suggestions tell me them. also don't be to harsh, I'm a beginner.

You may use std::string::find method to find the given character (returns the position of the character or std::string::npos if it is not found): http://www.cplusplus.com/reference/string/string/find/

Example:

std::string str = "Hello.World";

auto found = str.find('.');
if (found != std::string::npos)
    std::cout << "Period found at: " << found << std::endl;

In your case, it could look as such:

innum = (secretword.find(guess) != std::string::npos);

Try string.find .

auto i =secretword.find(guess); gives you the index. If it equals std::string::npos it isn't there.

Also, std::string has a size() method you can rrplace your letter counting with.

There are a few ways of approaching this. If you actually want to simply find if a string contains a character, the following works:

char needle;
haystack.find(needle) != string::npos;

As others have correctly answered. However, in this case, I think you are looking for something slightly different. Its a game of hangman so what you actually may want to do is show the portions of the word that the individual has actually guessed correctly.

Efficient

If we assume that: display is a string initialised with _ (or some character that is not a valid letter in a guessable word) as each character to the length of the string, you can work with this to partially display it.

If we have a map<char, vector<size_t>> that tells us exactly the indices that each character occurs at, which is easy to compute, we can take the guess and iterate through the corresponding vector of indices and replace characters in display with the actual character and then remove the character from the map . Once the map is empty, then the entire string has been guessed. You may also want to maintain a set of characters that have already been guessed to ensure that the same character can only be guessed once.

I will leave it as an exercise for you to implement this as it shouldn't be too difficult. If you'd like some pointers, let me know.

Less Efficient

This second way is less efficient, but since its a real-time game it won't make a difference unless these strings are really long.

Simply hold a copy of the string and iterate through it. Every time check which character you are checking against then replace the character at the index you are at in the _ word with the correct character. You can also keep a flag which checks whether or not any replacements happen and if not, you know that the character does not exist in the word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM