Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:15:00+00:00 2026-06-15T05:15:00+00:00

I was working on a program in C++ where I was supposed to play

  • 0

I was working on a program in C++ where I was supposed to play against a computer at hangman, and I am supposed to convert a particular .cpp file from using vectors to using Linked Lists. While I seem to almost be done, I keep getting this strange bug that causes my program to crash every time the computer guesses a letter incorrectly. I ran the program through the debugger and a message saying something along the lines of “program received SIGSEGV, segmentation fault” pops up along with this information in the call stack:

libstdc++6!_ZNSsC1ERKSs()
Guesser::guessACharacter(this=0x28feac) Line 56
main(argc=1, argv=0x7725a8)             Line 30

The crash seems to lead back to a problem with line 9 of my Guesser::guessACharacter function, where I try to put information from the current node into a temporary variable. It looks like this:

string word = current->data;

Here is the full code:

#include "guesser.h"
#include "game.h"

#include <iostream>
#include <fstream>


using namespace std;

const std::string Guesser::alphabet = "abcdefghijklmnopqrstuvwxyz";


// Initialize the guesser for a game wit hthe indicated wordlength,
// using words from an indicated file.
Guesser::Guesser (int wordLength, const char* wordListFilename)
{
    for (int i = 0; i < 26; ++i)
        charactersTried[i] = false;

    string word;
    ifstream in (wordListFilename);
    while (in >> word)
    {
        if (word.size() == wordLength)
        {
            // word is of desired length
            if (word.find_first_not_of(alphabet) == string::npos) {
                // word contains only lowercse alphabetics
                //cerr<<"data before possible soln.:/n"<<possibleSolutions->data<<endl;
                possibleSolutions->data = word;
                //cerr<<"data after possible soln.:/n"<<possibleSolutions->data<<endl;
                possibleSolutions = possibleSolutions -> next;
            }
        }
    }
    in.close();

}


/**
 * Scan the words that are possible solutions so far, counting, for
 * each letter not already tried, the number of words with that letter.
 * Guess the letter that occurs in the most words.
 */
char Guesser::guessACharacter()
{
    int counts[26];
    for (int i = 0; i < 26; ++i)
        counts[i] = 0;

    // Count the number of words in which each letter can be found
    for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
    {
        //cerr<<"data before possible soln.:/n"<<possibleSolutions->data<<endl;
        string word = current->data;
        //cerr<<"data in word:/n"<<word<<endl;
        for (char c = 'a'; c <= 'z'; ++c)
        {
            if (!charactersTried[c- 'a'])
            {
                // Character c has not been tried yet
                if (word.find(c) != string::npos)
                    // c is in this word
                    ++counts[c - 'a'];
            }
        }
    }

    // Find the character that occurs in the most words
    char guess = ' ';
    int maxSoFar = -1;
    for (char c = 'a'; c <= 'z'; ++c)
    {
        if (counts[c - 'a'] > maxSoFar)
        {
            guess = c;
            maxSoFar = counts[c - 'a'];
        }
    }


    if (maxSoFar <= 0)
    {
        guess = 'a';
        while (charactersTried[guess-'a'])
            ++guess;
    }

    charactersTried[guess-'a'] = true;
    return guess;
}


/**
 * Following a successful guess of a letter in the word, make a pass through
 * the possibleSolutions, removing all words that do not contain the
 * guess character in the positions indicated in wordSoFar.
 */
void Guesser::characterIsInWord (char guess, const string& wordSoFar)
{
    ListNode* remainingSolutions = 0;
    for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
    {
        string wd = current-> data;
        bool OK = true;
        for (int k = 0; OK && k < wordSoFar.size(); ++k)
        {
            if (wordSoFar[k] == guess)
            {
                if (wd[k] != guess)
                {
                    OK = false;
                }
            }
        }
        if (OK)
        {
            //cerr << "Keeping " << wd << endl;
            remainingSolutions->data = wd;
            //possibleSolutions->next = remainingSolutions;
        }
    }
    possibleSolutions = remainingSolutions;


}


/**
 * Following a mistaken guess of a letter in the word, make a pass through
 * the possibleSolutions, removing all words that contain the
 * guess character.
 */
void Guesser::characterIsNotInWord (char guess)
{
    ListNode* remainingSolutions;
    for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
    {
        string wd = current->data;
        if (wd.find(guess) == string::npos)
        {

            remainingSolutions = new ListNode(wd, remainingSolutions);
        }
    }
    possibleSolutions = remainingSolutions;

}


/**
 * Guesser has lost the game. Look at the provider's actual word
 * and gripe a bit about losing.
 */
void Guesser::admitToLoss (std::string actualWord, const string& wordSoFar)
{
    bool match = actualWord.size() == wordSoFar.size();
    for (int i = 0; match && i < actualWord.size(); ++i)
    {
        match = wordSoFar[i] == Game::FILL_CHARACTER ||
        wordSoFar[i] == actualWord[i];
    }
    if (!match)
    {
        cout << "Ummm...your word '" << actualWord
        << "' does not match the patterh '"
        << wordSoFar <<"'.\nDid you make a mistake somewhere?"
        << endl;
    }
    else
    {
        for (int i = 0; match && i < actualWord.size(); ++i)
        {
            if (wordSoFar[i] == Game::FILL_CHARACTER
                && charactersTried[actualWord[i]-'a'])
            {
                cout << "Did you forget to mention the '"
                << actualWord[i]
                << "' in position " << i+1 << "?"
                << endl;
                return;
            }
        }

        for (ListNode* current = possibleSolutions;(!match) && current !=
             NULL; current = current-> next)
        {
            match = (actualWord == current->data);
            current = current->next;
            match = match && (current != 0);
        }
        //match = match && (current != 0);
        if (match)
        {
            cout << "OK, I might have guessed that eventually." << endl;
        }
        else
        {
            cout << "Interesting, I don't know that word. Are you sure you\n"
            << "spelled it correctly?." << endl;
        }

    }
}

I feel like the answer to this is staring me right in my face, but I just can’t figure it out. I was wondering if someone else can easily spot what I’m doing wrong here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:15:01+00:00Added an answer on June 15, 2026 at 5:15 am

    In the following code snippet

    ListNode* remainingSolutions = 0;
    for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
    {
            ....
            if (wordSoFar[k] == guess)
            {
                if (wd[k] != guess)
                {
                    OK = false;
                }
            }
        }
        if (OK)
        {
            remainingSolutions->data = wd;
        }
    }
    possibleSolutions = remainingSolutions;
    

    remainingSolutions will be 0 or NULL if accessed and that can be one of the reasons.

    Also at the end it will assign NULL to possibleSolutions(which I assume is the head of linkedlist) so the next time you access it, maybe by calling guessACharacter(), it will cause segmentation fault.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent many hours working on this program that is supposed to play
I'm working on a program for class that reads data from a file, the
Working on a school project, the program is supposed to read from a text
is there any small working program for recieving from and sending data to client
Im working on a program to send and recieve SMS using a GSM modem
I'm working on a program that's supposed to represent a graph. My issue is
I've been working on a program that is supposed to output the winning status
The program that I am working on takes a file and parses it line
I'm working on a program that takes a redirected file as input. For example,
I am working on a program that is supposed to insert hundreds of rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.