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.

Dani AI

Generated

Thread context: a Hangman implementation using C-style strings and a need to detect when a guessed character is absent so it can be recorded as a wrong guess. and already pointed to useful low-level tools (STL std::find / std::string::find and strchr). The following gives a compact, maintainable pattern that avoids duplicate penalties and handles common pitfalls.

A robust pattern:

  • Keep the secret word as a std::string (or construct one from the C array).
  • Track wrong guesses in a container that supports fast membership tests (std::unordered_set<char> or std::bitset<26> for letters).
  • On each guess: normalize (e.g., lowercase with std::tolower cast to unsigned char), then test membership in the secret word. If found → reveal matching positions. If not found and not already in the wrong-guesses container → insert and increment misses. If already in wrong-guesses → treat as a repeat (no additional penalty).

Example (minimal):

#include <string>
#include <unordered_set>
#include <cctype>

// normalize single ASCII char safely
auto normalize = [](char c){ return static_cast<char>(std::tolower(static_cast<unsigned char>(c))); };

// usage inside game loop:
std::string secret = "hangman";          // or std::string(secretCArray)
std::unordered_set<char> wrongGuesses;
int misses = 0, maxMisses = 6;
char rawGuess = 'A';
char g = normalize(rawGuess);

if (secret.find(g) != std::string::npos) {
    // reveal positions in displayed word
}
else if (wrongGuesses.count(g)) {
    // already guessed; no extra penalty
}
else {
    wrongGuesses.insert(g);
    ++misses;
}

Notes and cautions: prefer std::string for simplicity; when staying with C arrays, strchr or std::find (as mentioned by and ) work fine. For case-insensitive play, normalize both input and the stored secret. For non-ASCII/UTF-8 input, character-by-character logic needs proper Unicode handling rather than std::tolower.

Recommended Answers

All 6 Replies

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.