dimanche 16 février 2020

What is causing a "bad_alloc" error in my code? [closed]

I'm trying to make a game called Liar's Dice in c++. For some reason, I can't get the game to move on to the next player if all players accept the bet. And if liar is called, I get the correct output, but in both cases, I get a "bad_alloc" error message and it points to the "return this->name" in the getName() function. Here is my code so far. It's broken up into header and source files.

#pragma once



#include <iostream>

#include <time.h>

#include <cstdlib>

#include "Player.h"

#include "Dice.h"



class Game

{

private:

    Player *players;



    int currentPlayer;

    int countPlayers;

    Dice dice;





public:

    Game(int numPlayers);

    void readPlayerName();

    void assignDice();

    void startGame();

    void makeBet();



    bool winner(int *index);

    void playEachRound(int index);

    void eachPlayerGame(int i);



};



#pragma once



#include <iostream>

#include <time.h>

#include <cstdlib>

#include <string>

#include "GameLoop.h"

#include "Dice.h"





using namespace std;



class Player

{

private:

    string name;

    int point;

public:

    Player();

    Player(string playerName);

    void setName(string playerName);

    string getName();

    void setScore(int point);

    int getScore();

};



#pragma once



#include <iostream>

#include <time.h>

#include <cstdlib>

#include <vector>

#include "GameLoop.h"

#include "Player.h"



using namespace std;



class Dice

{

private:

  int playerDice[5];



public:

  vector <int> allDice;

  void playDice();

  void roll();

  void show();

  void showAllDice();

  int result;

};



#include <iostream>

#include <time.h>

#include <cstdlib>

#include <string>

#include <fstream>

#include "GameLoop.h"



using namespace std;



void printRules()

{

  string line;

  fstream rules("LiarsDiceRules.txt", ios::in);



  cout << "\t\tLIAR'S DICE RULES\n";

  cout << "==============================================================\n\n";



  if (rules.is_open())

  {

    while (getline(rules, line))

    {

      cout << line << endl;

    }



    rules.close();

    cout << "==============================================================\n\n";

  }



  else

  {

    cout << "File Not Found!";

  }

}



int main()

{

  srand(time(NULL));

  int numPlayers;



  cout << "\t\t******** WELCOME TO LIAR'S DICE ********\n\n";



  printRules();



  cout << "How many players will there be? : ";

  cin >> numPlayers;



  while (numPlayers < 2)

  {

    cout << "There must be at least 2 players! Try again : ";

    cin >> numPlayers;

  }



  Game game(numPlayers);

  game.assignDice();

  game.readPlayerName();

  game.startGame();



  system("PAUSE");



  return 0;

}



#include <iostream>

#include <time.h>

#include <cstdlib>

#include <time.h>

#include "GameLoop.h"

#include "Player.h"

#include "Dice.h"



using namespace std;



void Dice::playDice()

{

    for (int i = 0; i < 5; i++)

    {

        playerDice[i] = 0;

    }

}



void Dice::roll()

{

    for (int i = 0; i < 5; i++)

    {

        playerDice[i] = ((rand() % 6) + 1);



        allDice.push_back(playerDice[i]);

    }

}



void Dice::show()

{

    for (int i = 0; i < 5; i++)

    {

        cout << playerDice[i];

    }

}



void Dice::showAllDice()

{

    for (unsigned int i = 0; i < allDice.size(); i++)

    {

        cout << allDice.at(i);

    }

}





#include <iostream>

#include <time.h>

#include <cstdlib>

#include <time.h>

#include "GameLoop.h"

#include "Player.h"

#include "Dice.h"



using namespace std;



Player::Player()

{

    name = " ";

    point = 0;

}



Player::Player(string playerName)

{

    this->name = name;

    point = 0;

}



void Player::setName(string playerName)

{

    this->name = playerName;

}



string Player::getName()

{

    return this->name;

}



void Player::setScore(int point)

{

    this->point = point;

}



int Player::getScore()

{

    return point;

}



#include <iostream>

#include <time.h>

#include <cstdlib>

#include <time.h>

#include <string>

#include <vector>

#include "GameLoop.h"

#include "Player.h"

#include "Dice.h"



using namespace std;



Game::Game(int numPlayers)

{

    players = new Player[numPlayers];

    countPlayers = numPlayers;



}



void Game::readPlayerName()

{

    string name;



    for (int i = 0; i < countPlayers; i++)

    {

        cout << "Enter player " << (i + 1) << "'s name : ";

        cin >> name;

        players[i].setName(name);

    }

}



void Game::assignDice()

{

    for (int i = 0; i < countPlayers; i++)

    {

        dice.roll();

    }

}



void Game::makeBet()

{

    int betNumberOfDice;

    int betFaceValueOfDice;



    cout << "Make A Guess!\n";



    cout << "Enter The Number Of Dice You Think There Are : ";

    cin >> betNumberOfDice;



    cout << "Enter The Face Value Of Those Dice : ";

    cin >> betFaceValueOfDice;



    cin.clear();

    cin.ignore();



    int option;

    int countDice = 0;

    int winPoint = 0;



    for (int i = 0; i < sizeof(players[i]); i++)

    {

        if (i == currentPlayer)

        {

            continue;

        }



        else

        {

            cout << players[i].getName() << ", Do You Accept " << players[currentPlayer].getName() << "'s Guess ? \n" << "(1) Accept(2) Liar!\n";

            cin >> option;



            if (option == 1)

            {

                cout << "Accepted\n";

                winPoint = 0;

                players[currentPlayer].setScore(winPoint);

                players[i].setScore(winPoint);



            }



            else if (option == 2)

            {

                cout << "LIAR!\n\n";



                int result = 0;



                cout << "All Dice Rolled : \n";

                dice.showAllDice();



                for (int vecNum : dice.allDice)

                {

                    if (vecNum == betFaceValueOfDice)

                    {

                        countDice += 1;

                    }

                }



                if (countDice == betNumberOfDice)

                {

                    cout << endl << players[currentPlayer].getName() << " Guessed Correctly!\n";

                    cout << players[currentPlayer].getName() << " WINS!";

                    winPoint = 1;

                    players[currentPlayer].setScore(winPoint);

                }



                else

                {

                    cout << endl << players[currentPlayer].getName() << " Is A LIAR!!!!!\n";

                    cout << players[i].getName() << " WINS!";

                    winPoint = 1;

                    players[i].setScore(winPoint);

                }



            }



        }

    }



}



bool Game::winner(int* index)

{

    for (int i = 0; i < countPlayers; i++)

    {

        if (players[i].getScore() == 1 || players[currentPlayer].getScore() == 1)

        {

            *index = i || currentPlayer;

            return true;

        }

    }

    return false;

}



void Game::eachPlayerGame(int i)

{

    currentPlayer = i;



    cout << players[i].getName() << " rolled : ";

    dice.show();

    cout << endl;

    makeBet();





    if (players[i].getScore() == 0 && players[currentPlayer].getScore() == 0)

    {

        cout << "Next Players Turn...";

    }



}



void Game::playEachRound(int index)

{

    for (int i = index; i < countPlayers; i++)

    {

        eachPlayerGame(i);

    }

}





void Game::startGame()

{

    int index = 0;

    int count = 0;

    int i = 0;



    while (!winner(&index))

    {

        playEachRound(0);

    }



    for (int i = index + 1; i < countPlayers; i++)

    {

        eachPlayerGame(i);

        count++;

    }



    if (count != countPlayers - 1)

    {

        for (i = 0; i < index; i++)

        {

            eachPlayerGame(i);

        }

    }

}

Sorry for making such a long post, but I've only been using C++ for a few weeks. I'm not sure what in my code is causing an issue. Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire