Sign in to follow this  
NUCLEAR RABBIT

System Crash! (C++)

Recommended Posts

Ello! I am creating a hangman game using the new things Ive learned with classes, memory allocation, statics, and other things. The problem that seems to be happening is whenever i enter a letter in the word, the game crashes, if i enter an incorrect letter, everything works fine. Please help. Hangman Source:
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

class Player
{
      public:
             Player(const string TheWord); 
             ~Player(); 
             string SetWord(); 
             void SetGuess(); 
             void GetUsed(); 
             void GetSoFar(); 
             void GetNumGuesses(const string& TheWord);
             void CheckWin(const string& TheWord, bool& win);
             void CheckGuess(const string& TheWord); 
             static int m_NumGuesses; 
      private:
             char m_Guess;
             vector<char> * m_Used;
             string * m_SoFar;
}; 

int Menu();
void Pause();
string SetWord();

int Player::m_NumGuesses = 0;

int main()
{
    string TheWord = SetWord();
    
    Player player(TheWord);
    
    bool done = false;
    bool win = false;
    
    while(!done)
    {
        switch(Menu())
        {
            case 0:
                cout << "\n\nBye!\n";
                Pause();
                done = true;
                break;
            case 1:
                do
                {
                    player.GetUsed();
                    player.GetNumGuesses(TheWord);
                    player.GetSoFar();
                    player.SetGuess();
                    player.CheckGuess(TheWord);
                }while(!win);
                break;
            default:
                cout << "\n\nInvalid Option.\n\n";
                Pause();
        }
    }
    return 0;
    
}

//------------------------------------------------------------------------------
// Name: GetNumGuesses
// Desc: Prints the amount of guesses left
//------------------------------------------------------------------------------

void Player::GetNumGuesses(const string& TheWord)
{
    cout << "\nAmount of Guesses left: " << (TheWord.size() - m_NumGuesses)
         << endl;
}

//------------------------------------------------------------------------------
// Name: CheckWin()
// Desc: checks to see if user has won
//------------------------------------------------------------------------------

void Player::CheckWin(const string& TheWord, bool& win)
{
    if(*m_SoFar == TheWord)
    {
        cout << "\n\nYou Won!!";
        Pause();
        win = true;
    }
    if((TheWord.size() - m_NumGuesses) <= 0)
    {
        cout << "\n\nYou Lost!!";
        Pause();
        win = true;
    }
}

//------------------------------------------------------------------------------
// Name: GetSoFar()
// Desc: Prints out the letters guesses correcty so far
//------------------------------------------------------------------------------

void Player::GetSoFar()
{
    cout << "\nThe Word So Far: " << *m_SoFar << endl << endl;
}

//------------------------------------------------------------------------------
// Name: CheckGuess()
// Desc: Checks to see if the users guess is correct
//------------------------------------------------------------------------------

void Player::CheckGuess(const string& TheWord)
{
    bool check = false;
    
    do
    {
         for(int i = 0; i != TheWord.size(); ++i)
         {
                 if(TheWord.find(m_Guess) == string::npos)
                 {
                      m_Used->push_back(m_Guess);
                      check = true;
                      m_NumGuesses++;
                      break;
                 }
                 else if(m_Guess == TheWord[i])
                 {
                      m_SoFar[i] = m_Guess;
                      check = true;
                      break;
                 }
         }
    }while(!check);
}

//------------------------------------------------------------------------------
// Name: GetUsed()
// Dsc: Prints out the letters that have been guessed
//------------------------------------------------------------------------------

void Player::GetUsed()
{
    cout << "\nUsed Letters: "; 
    
     for(vector<char>::const_iterator cIter = m_Used->begin(); cIter != m_Used->end(); ++cIter)
     {
          cout << " " << *cIter << " ";
     }
     
     cout << endl;
}

//------------------------------------------------------------------------------
// Name: SetGuess()
// Desc: Gets the users guess
//------------------------------------------------------------------------------

void Player::SetGuess()
{    
     cout << "\nPlease enter a letter guess: ";
     cin >> m_Guess;
}

//------------------------------------------------------------------------------
// Name: Menu()
// Desc: Gets the user's desired action
//------------------------------------------------------------------------------

int Menu()
{
    system("CLS");
    
     int menu= 0;
     
     cout << "Welcome to Hangman\n"
          << "Programed by Brandon Wall\n\n"
          << "Menu Items:\n\n"
          << "0 = Quit\n"
          << "1 = New Game\n\n"
          << "Menu Selection: ";
     cin >> menu;
     
     system("CLS");
     
     return menu;
}

//------------------------------------------------------------------------------
// Name: Pause()
// Desc: Waits for the user to continue
//------------------------------------------------------------------------------

void Pause()
{
     cout << "\nPress [ENTER] to continue.";
     cin.ignore();
     cin.get();
}

//------------------------------------------------------------------------------
// Name: Player()
// Desc: Allocates memry on the heap for the private vectors
//------------------------------------------------------------------------------

Player::Player(string TheWord)
{
    m_Used = new vector<char>;
    m_SoFar = new string(TheWord.size(), ' ');
}

//------------------------------------------------------------------------------
// Name: ~Player()
// Desc: Free's all the memory on the heap
//------------------------------------------------------------------------------

Player::~Player()
{
    delete m_Used;
    delete m_SoFar;
}

//------------------------------------------------------------------------------
// Name: SetWord()
// Desc: Sets the word the user to guess
//------------------------------------------------------------------------------

string SetWord()
{
       vector<string> words;
       words.push_back("buiness");
       words.push_back("computer");
       words.push_back("evergreen");
       words.push_back("designer");
       words.push_back("dynamite");
       
       random_shuffle(words.begin(), words.end());
       
       return words[0];
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
I won't give you a complete answer, only a hint: what is the difference between
  std::string s("something");
s[5] = 'T';
and
  std::string *s = new std::string("something");
s[5] = 'T';
?


you have to make the pointer s dereferenced. (*s)[5] = 'T';

Thanks for the hint and not just giving me the answer, buddy [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Quote:
Original post by Emmanuel Deloget
I won't give you a complete answer, only a hint: what is the difference between
  std::string s("something");
s[5] = 'T';
and
  std::string *s = new std::string("something");
s[5] = 'T';
?


you have to make the pointer s dereferenced. (*s)[5] = 'T';

Thanks for the hint and not just giving me the answer, buddy [smile]


You're welcome!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this