Hello Dani Web,

I have written a program that needs to run on Ubuntu 14.0. It runs on Windows 10 and will compile in Replit. But I get this error in Replit. Can anyone figure out why? I assume it's in relation to my pointer "highScore". Can anyone figure out why. Also where is the most efficient place to delete "highScore."

repit_error.JPG

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// My struct to control the game
struct Game {

    int lo = 10;
    int hi = 20;
    int seed = 3;
    int randNum = 0;
    int randNum2 = 0;
    int humanScore = 0;
    int computerScore = 0;
    int overallHumanScore = 0;
    int overallComputerScore = 0;
    bool humanTurn = true;
    bool gameOver = false;
    bool validChoice = false;
    string playerName;
    int choice = 0;
    int* highScore = 0;
    char yesno = ' ';

};

int D6(Game &game1);
int RandomNumber(Game &game1);
int ProtectRandomNumber(Game &game1);
int ProtectRandomNumber2(Game &game1);
int fixed_seed(Game &game1);
void get_name(Game &game1);
void program_greeting();
int get_game_choice(Game &game1);
int protect_choice(Game &game1);
void pig_game(Game &game1);
int high_score(Game &game1);


int main()
{
    Game game1;

    get_name(game1);
    program_greeting();
    cout << "C1 Spec: " << fixed_seed(game1) << endl;
    cout << "A2 Spec: " << RandomNumber(game1) << endl;
    cout << "A3 Spec: " << ProtectRandomNumber(game1) << endl;
    cout << "A4 Spec: " << ProtectRandomNumber2(game1) << endl;
    do {
        while (!game1.gameOver)
        {
            pig_game(game1);
        }

        cout << "B3 & B4 Spec  - High score: " << high_score(game1) << endl;
        cout << "Would you like to play again? Press Y to continue or any ";
        cout << "other key to quit: ";
        cin >> game1.yesno;
        if ((game1.yesno == 'y') || (game1.yesno == 'Y')) {
            game1.yesno == 'Y';
            game1.gameOver = false;
        }
        else {
            exit(0);
        }
        delete game1.highScore;
        cout << high_score(game1) << endl;
    } while (game1.yesno != 'Y');

    return 0;
}


// Spec A1 - random number generator & D6
int D6(Game &game1)
{
    srand(time(NULL));
    game1.randNum = rand() % 6 + 1;
    return game1.randNum;
}

// Spec A2 - random number generator between lo and high
int RandomNumber(Game &game1)
{

    srand(time(NULL));
    game1.randNum2 = rand() % (game1.hi - game1.lo) + game1.lo;
    return game1.randNum2;
}

// Spec A3 - random number generator between lo and high with error checking -1
int ProtectRandomNumber(Game &game1)
{
    game1.lo = 1;
    game1.hi = 100;

    if ((game1.lo >= game1.hi) || (game1.lo < 1) || (game1.hi > 100))
        return -1;
    else
        game1.randNum2 = rand() % (game1.hi - game1.lo) + game1.lo;
    return game1.randNum2;

}

// Spec A4 - random number generator between lo and high with error checking -1 & -2
int ProtectRandomNumber2(Game &game1)
{
    game1.lo = 1;
    game1.hi = 100;

    if ((game1.lo >= game1.hi) || (game1.lo < 1) || (game1.hi > 100))
        return -1;
    else
        game1.randNum2 = rand() % (game1.hi - game1.lo) + game1.lo;

    if (game1.randNum2 > 100)
        return -2;
    else
        return game1.randNum2;

}

// Spec C1 - fixed seed function
int fixed_seed(Game &game1)
{
    srand(game1.seed);
    game1.randNum = rand() % 6 + 1;
    return game1.randNum;
}

// C2 Spec - Student/player name
void get_name(Game &game1)
{
    cout << "Please enter your first and last name >> ";
    getline(cin, game1.playerName);

}

// Spec B2 - game greeting with due date
void program_greeting()
{
    cout << endl;
    cout << "Welcome to Pig! ~ Due 09.04.2022" << endl;
    cout << "--------------------------------" << endl;
}

// Specs C3 - Numeric menu
int get_game_choice(Game &game1)
{
    cout << "-------------------------------" << endl;
    cout << "1. Roll die" << endl;
    cout << "2. Hold" << endl;
    cout << "3. Quit" << endl;
    cout << "-------------------------------" << endl;
    cout << "Enter your choice: ";
    cin >> game1.choice;

    return game1.choice;
}


// Spec C4 - Bulletproof menu
int protect_choice(Game &game1)
{
    if (game1.choice == 1)
    {

        cout << "You rolled a " << D6(game1) << "." << endl;
        if (game1.randNum != 1)
        {
            game1.humanScore += game1.randNum;
            cout << "Your turn score is " << game1.humanScore << "." << endl;
        }
        else
        {
            cout << "Your turn score is " << game1.humanScore << "." << endl;
            game1.humanTurn = false;
            game1.validChoice = true;
        }
    }
    else if (game1.choice == 2)
    {
        game1.overallHumanScore += game1.humanScore;
        cout << "Your turn score is " << game1.humanScore << "." << endl;
        cout << "Your overall score is " << game1.overallHumanScore << "." << endl;
        game1.humanTurn = false;
        game1.validChoice = true;
    }
    else if (game1.choice == 3)
    {
        exit(0);
    }
    else
    {
        cout << "Invalid choice. Please enter 1, 2, or 3." << endl;
    }
}


// The actual game 
void pig_game(Game &game1)
{

    if (game1.humanTurn)
    {
        cout << endl;
        cout << game1.playerName << "'s game." << endl;
        game1.humanScore = 0;
        game1.validChoice = false;
        while (!game1.validChoice)
        {
            get_game_choice(game1);
            cout << endl;
            protect_choice(game1);

        }
    }
    else
    {
        cout << "Computer's turn." << endl;
        game1.computerScore = 0;
        game1.validChoice = false;
        while (!game1.validChoice)
        {

            game1.randNum = rand() % 3 + 1;
            if (game1.randNum == 1)
            {
                game1.overallComputerScore += game1.computerScore;
                cout << "The computer's turn score is " << game1.computerScore << "." << endl;
                cout << "The computer's overall score is " << game1.overallComputerScore << "." << endl;
                game1.humanTurn = true;
                game1.validChoice = true;
            }
            else
            {
                game1.randNum = rand() % 6 + 1;
                cout << "The computer rolled a " << game1.randNum << "." << endl;
                game1.computerScore += game1.randNum;
                cout << "The computer's turn score is " << game1.computerScore << "." << endl;
            }
        }
    }
    if (game1.overallHumanScore >= 100)
    {
        game1.gameOver = true;
        cout << "-------------------------------" << endl;
        cout << game1.playerName << " wins!" << endl;
        cout << "~ Final score ~ " << endl;
        cout << game1.playerName << ": " << game1.overallHumanScore << endl;
        cout << "Computer: " << game1.overallComputerScore << endl;
    }
    else if (game1.overallComputerScore >= 100)
    {
        game1.gameOver = true;
        cout << "-------------------------------" << endl;
        cout << "The computer wins!" << endl;
        cout << "~ Final score ~" << endl;
        cout << game1.playerName << ": " << game1.overallHumanScore << endl;
        cout << "Computer: " << game1.overallComputerScore << endl;
    }

}

// Spec B3 - Store high score on the heap and delete afterwards.
int high_score(Game &game1)
{
    game1.highScore = game1.overallHumanScore > game1.overallComputerScore ?
        new int(game1.overallHumanScore) : new int(game1.overallComputerScore);

    return *game1.highScore;

}

That's what a debugger is for. Run it in the debugger, set a breakpoint at your last output statement, then single step, checking your pointer references, until you find the one that is invalid. Then you have to work backwards and find out why it is invalid. It could be something as simple as a variable not being initialized.

On line 69 of the code snippet you posted, you deleted game1.highScore.

Then, on line 70, you pass game1 into the function high_score().

When I look in that function beginning on line 269, I see you are setting game1.highScore to a value, and then returning a pointer to game1.highScore.

I'll be honest with you. I haven't looked at a single line of C++ code in twenty years. But, I have to ask, why delete game1.highScore, and then on the next line, set it to a new value. Can't you just overwrite its value? It looks like you're passing game1 into high_score() by reference so you should be overwriting the value of game1.highScore already, no?

What's the advantage to returning a pointer instead of just returning game1.highScore? Why is highScore a pointer to begin with? My stab-in-the-dark guess is that you're getting an error message because you're setting a value to game1.highScore inside the function to be an integer object, yet highScore has been defined as a pointer. And then you're returning that pointer.

I wish I could be more help, but I have been doing PHP for the past 20 years, which has syntax very similar to C++, but doesn't use pointers so explicitly.

commented: The high score after the deletion was to debug the program to see if the high score above was actually deleted +0

Why is highScore a pointer?

int* highScore = 0;
commented: My assignment specifically requires it to be +0
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.