I made a program, hangman. And I am trying to complete it. I have succesfully been able to compare the input letter by all the characters of the word to be guessed (which is saved in an char array) and display it. But now, I can not figure out how to detect the absence of a character in a srting. So that I can add it to the array of the wrongly guessed letters. Your help will be greatly appreciated.

If you do not find it, it is not there.

are you using the string class or are you using a char array?

I am using char array. The c++ strings not the strings strings. I need to keep check of the wrongly guessed characters so that I can increment on the chances and once the user has used 6 chances, he loses the game. Also, I need to keep track of the wrongly guesses letters for if the user guesses the wrong character say 'a', he has already guessed it, so that I can notify him that this guess has already been made.

If you're using a std::string then you can use its find method, which returns std::string::npos if the character you're looking for isn't there. If you're using a char array then you can use std::find from the STL <algorithm> library:

#include <algorithm>
#include <iostream>

int main()
{
    // Make some char arrays to test on
    const char* word = "hangman";
    const size_t length = 7;
    const char* end = word + length;

    // Define a letter to look for
    const char letterToFind = 'z';

    // Use STL find to get a pointer to the character, or to end if the character isn't found
    const char* p = std::find( word, end, letterToFind );

    // Print some results
    std::cout << "Letter " << ( p == end ? "not " : "" ) << "found" << std::endl;

    return 0;
}

If you can, I'd use std::string instead of a char array.

Thank you for your help everyone. Also, I have been able to use the bool operator to see if a charatcter doesn't exist in my word. The boolc hanges to true if the character is found else remains false.

The simplest way is to use the C function strchr(const char* source, char target). It will return the pointer to the character if found in the string, or null if not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.