How do you fix the Visual Studio 2022 Error code: 0xC0000005: Access violation reading location 0x0000000000000000.

Edward Williams 21 Reputation points
2023-01-03T03:08:57.11+00:00

I keep getting this error code every time I try to run the debugger in Visual Studio 2022.

An exception is thrown at 0x00007FF923F14961 (ucrtbased.dll) in IT-312 - Final Project - Farkle - Edward A Williams.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

I have tried deleting the .dll file and downloading and installing another copy from dll-files.com but that doesn't fix the issue.

When I run the code, it fails to load and brings up an unhandled exception for ucrtbased.dll that I have been unable to do anything about. Anything I try doesn't seem to work.

/*
Edward Anthony Williams
IT-312
27 November 2022
*/

include <random> // random is needed to generate random numbers for the dice.

include <iostream>

include <string>

include <cstdlib>

include <ctime>

using namespace std;
using std::string;
using std::cin;
using std::cout;

// This is the set of classes that will be used in the program.
class Winner;
class Player;

int i;
// These variables will represent the dice for the game of Farkle.
int die1;
int die2;
int die3;
int die4;
int die5;
int die6;

// This variable is used to track the input of the user to decide which dice to keep.
int KeepDiceChoice = 0;

// This string variable is used to track whether the dice selected are a set.
string DiceSet = "";

// These variables are used to track the number of dice in a set that are kept.
int DiceSet1 = 0;
int DiceSet2 = 0;
int DiceSet3 = 0;
int DiceSet4 = 0;
int DiceSet5 = 0;
int DiceSet6 = 0;

// These variables are used to track the dice kept that were not in a set.
int Dice1 = 0;
int Dice2 = 0;
int Dice3 = 0;
int Dice4 = 0;
int Dice5 = 0;
int Dice6 = 0;

// These variables are used to track the number of dice in a set that are kept.
string KeepDice1;
string KeepDice2;
string KeepDice3;
string KeepDice4;
string KeepDice5;
string KeepDice6;

// this is the int variable used to select the amount of players playing the game. For this game there will be a limiter where only 2-4 players can play.
int NumPlayers = 0;
string PlayerInput = 0;

int PlayerTempScore = 0; // This player score keeps the score of the current players turn.
int PlayerScore[4] = { 0, 0, 0, 0, }; // This array of int variables is to help keep track of the player's points.

int NumDiceKept = 0; // This variable is used to track the number of dice kept.
int NumDiceSet = 0; // This integer stores the number of dice in a set.

string BeginGame = ""; // String to decide whether the game should begin or exit.
string PlayerTurn = ""; // This array keeps track of whose turn it is.
string Players[4] = { "Player 1", "Player 2", "Player 3", "Player 4" }; // This array of string variables is used to hold the name of the players.
string CurrentPlayer = ""; // This variable is used to keep track of the current player.

void DisplayRules()
{
cout << "Welcome to Farkle!" << "\n";
cout << "The rules for the game of farkle are as follows:" << "\n\n";
cout << "1. The game is played with six dice." << "\n";
cout << "2. The player must roll all six dice at least once per turn." << "\n";
cout << "3. You will have 3 rolls to get a score." << "\n";
cout << "4. Before a player can score their points for the round they mus initially obtain" << "\n";
cout << " 1000 points in a single round." << "\n";
cout << "5. Once the player has obtained 1000 points in a single round they can choose" << "\n";
cout << " to keep their points or roll again." << "\n";
cout << "6. Only once you have acquired 1000 points and kept them for the round you got" "\n";
cout << " them can you choose to keep any points obtained in future rolls." << "\n";
cout << "7. The first player rolls all six dice at the same time and sets aside any point dice." << "\n";
cout << "8. A farkle happens when a player rolls the dice and doesn't obtain any point dice in the roll." << "\n";
cout << "9. If a player rolls a farkle they lose all points they have acquired in the round" << "\n";
cout << " and the turn passes to the next player." << "\n";
cout << "10. If a player rolls a farkle and has not acquired 1000 points in the round they lose all points" << "\n";
cout << " they have acquired in the game." << "\n";
cout << "11. If a player does not wish to risk losing the points they have obtained in the round," << "\n";
cout << " they may choose to stop rolling and count their score for the round as long as rule 3 is true." << "\n";
cout << "12. If during one turn, all six dice become point dice and are set aside, the player must roll all" << "\n";
cout << " six dice at least one more time, before stopping and keeping the points collected in the round." << "\n";
cout << "13. It may ocassionally be beneficial to roll some of the point dice with the non-point dice" << "\n";
cout << " in order to have a higher chance at obtaining a three of a kind. But at least 1 point die" << "\n";
cout << " must be set aside after each roll." << "\n";
cout << "14. The player who has reached the score limit of 10,000 is the winner." << "\n\n";
};

class Winner
{
private:
void DisplayScore()
{
for (int i = 0; i < NumPlayers; i++)
{
if (PlayerScore[i] >= 10000)
{
cout << Players[i] << " Wins!" << endl;
exit(0);
}
}
}
};

void RollDice() // This function rolls the dice depending on the amount of dice currently kept. Then it displays the dice roll to the player.
{
srand(time(NULL));
for (int i = 0; i < NumDiceKept; i++)
{
switch (i)
{
case 0:
die1 = rand() % 6 + 1;
cout << "Die 1: " << die1 << "\n";
break;
case 1:
die2 = rand() % 6 + 1;
cout << "Die 2: " << die2 << "\n";
break;
case 2:
die3 = rand() % 6 + 1;
cout << "Die 3: " << die3 << "\n";
break;
case 3:
die4 = rand() % 6 + 1;
cout << "Die 4: " << die4 << "\n";
break;
case 4:
die5 = rand() % 6 + 1;
cout << "Die 5: " << die5 << "\n";
break;
case 5:
die6 = rand() % 6 + 1;
cout << "Die 6: " << die6 << "\n";
break;
}
}
}

void KeepDice() // This function facilitates the process of keeping the dice the user selects.
{
if (die1 == 1 || die1 == 5 || die2 == 1 || die2 == 5 || die3 == 1 || die3 == 5 || die4 == 1 || die4 == 5 || die5 == 1 || die5 == 5 || die6 == 1 || die6 == 5)
{
cout << "What dice would you like to keep?\n";
cout << "\n";
cout << "Is this die part of a set?\n";
getline(cin, DiceSet);

    if (DiceSet == "Yes" || DiceSet == "yes")  
    {  
        cout << "Enter the dice that are in the set, one at a time.\n\n";  
        cin >> DiceSet1;  
        cout << "\n";  
        cin >> DiceSet2;  
        cout << "\n";  
        cin >> DiceSet3;  
        cout << "\n";  

        if (die1 == 1 && die2 == 1 && die3 == 1)  
        {  
            PlayerTempScore = PlayerTempScore + 1000;  
            NumDiceKept = (NumDiceKept + 3);  
        }  
        if (die1 == 2 && die2 == 2 && die3 == 2)  
        {  
            PlayerTempScore = PlayerTempScore + 200;  
            NumDiceKept = (NumDiceKept + 3);  
        }  
        else if (die1 == 3 && die2 == 3 && die3 == 3)  
        {  
            PlayerTempScore = PlayerTempScore + 300;  
            NumDiceKept = (NumDiceKept + 3);  
        }  
        else if (die1 == 4 && die2 == 4 && die3 == 4)  
        {  
            PlayerTempScore = PlayerTempScore + 400;  
            NumDiceKept = (NumDiceKept + 3);  
        }  
        else if (die1 == 5 && die2 == 5 && die3 == 5)  
        {  
            PlayerTempScore = PlayerTempScore + 500;  
            NumDiceKept = (NumDiceKept + 3);  
        }  
        else if (die1 == 6 && die2 == 6 && die3 == 6)  
        {  
            PlayerTempScore = PlayerTempScore + 600;  
            NumDiceKept = (NumDiceKept + 3);  
        }  


    }  

    else if (DiceSet == "No" || DiceSet == "no")  
    {  
        cout << "What dice would you like to keep?\n\n";  
        cin >> KeepDiceChoice;  
        cout << "\n";  

        if (KeepDiceChoice == 1)  
        {  
            if (Dice1 == 1 || Dice1 == 5)  
            {  
                cout << "Die 1: " << die1 << "\n";  
                cout << "Keep this die?\n";  
                getline(cin, KeepDice1);  
                if (KeepDice1 == "Yes" || KeepDice1 == "yes")  
                {  
                    NumDiceKept = (NumDiceKept + 1);  
                    if (die1 == 1)  
                    {  
                        PlayerTempScore = PlayerTempScore + 100;  
                    }  
                    else if (die1 == 5)  
                    {  
                        PlayerTempScore = PlayerTempScore + 50;  
                    }  
                }  
            }  
        }  

        else if (KeepDiceChoice == 2)  
        {  
            if (Dice2 == 1 || Dice2 == 5)  
            {  
                cout << "Die 2: " << die2 << "\n";  
                cout << "Keep this die?\n";  
                getline(cin, KeepDice2);  

                if (KeepDice2 == "Yes" || KeepDice2 == "yes")  
                {  
                    NumDiceKept = (NumDiceKept + 1);  
                    if (die1 == 1)  
                    {  
                        PlayerTempScore = PlayerTempScore + 100;  
                    }  
                    else if (die1 == 5)  
                    {  
                        PlayerTempScore = PlayerTempScore + 50;  
                    }  
                }  
            }  
        }  

        else if (KeepDiceChoice == 3)  
        {  
            if (Dice3 == 1 || Dice3 == 5)  
            {  
                cout << "Die 3: " << die3 << "\n";  
                cout << "Keep this die?\n";  
                getline(cin, KeepDice3);  

                if (KeepDice3 == "Yes" || KeepDice3 == "yes")  
                {  
                    NumDiceKept = (NumDiceKept + 1);  
                    if (die3 == 1)  
                    {  
                        PlayerTempScore = PlayerTempScore + 100;  
                    }  
                    else if (die3 == 5)  
                    {  
                        PlayerTempScore = PlayerTempScore + 50;  
                    }  
                }  
            }  
        }  

        else if (KeepDiceChoice == 4)  
        {  
            if (Dice4 == 1 || Dice4 == 5)  
            {  
                cout << "Die 4: " << die4 << "\n";  
                cout << "Keep this die?\n";  
                getline(cin, KeepDice4);  

                if (KeepDice4 == "Yes" || KeepDice4 == "yes")  
                {  
                    NumDiceKept = (NumDiceKept + 1);  
                    if (die4 == 1)  
                    {  
                        PlayerTempScore = PlayerTempScore + 100;  
                    }  
                    else if (die4 == 5)  
                    {  
                        PlayerTempScore = PlayerTempScore + 50;  
                    }  
                }  
            }  
        }  

        else if (KeepDiceChoice == 5)  
        {  
            if (Dice5 == 1 || Dice5 == 5)  
            {  
                cout << "Die 5: " << die5 << "\n";  
                cout << "Keep this die?\n";  
                getline(cin, KeepDice5);  

                if (KeepDice5 == "Yes" || KeepDice5 == "yes")  
                {  
                    NumDiceKept = (NumDiceKept + 1);  
                    if (die5 == 1)  
                    {  
                        PlayerTempScore = PlayerTempScore + 100;  
                    }  
                    else if (die5 == 5)  
                    {  
                        PlayerTempScore = PlayerTempScore + 50;  
                    }  
                }  
            }  
        }  

        else if (KeepDiceChoice == 6)  
        {  
            if (Dice6 == 1 || Dice6 == 5)  
            {  
                cout << "Die 6: " << die6 << "\n";  
                cout << "Keep this die?\n";  
                getline(cin, KeepDice6);  

                if (KeepDice6 == "Yes" || KeepDice6 == "yes")  
                {  
                    NumDiceKept = (NumDiceKept + 1);  
                    if (die6 == 1)  
                    {  
                        PlayerTempScore = PlayerTempScore + 100;  
                    }  
                    else if (die6 == 5)  
                    {  
                        PlayerTempScore = PlayerTempScore + 50;  
                    }  
                }  
            }  
        }  
    }  
    else  
    {  
        cout << "Invalid input. Please try again.\n\n";  
        KeepDice();  
    }  
}  

else  
{  
    cout << "Farkle!!!";  

    if (NumPlayers == 2)  
    {  
        if (PlayerTurn == Players[0])  
        {  
            PlayerTurn = Players[1];  
        }  
        else if (PlayerTurn == Players[1])  
        {  
            PlayerTurn = Players[0];  
        }  
    }  

    else if (NumPlayers == 3)  
    {  
        if (PlayerTurn == Players[0])  
        {  
            PlayerTurn = Players[1];  
        }  
        else if (PlayerTurn == Players[1])  
        {  
            PlayerTurn = Players[2];  
        }  
        else if (PlayerTurn == Players[2])  
        {  
            PlayerTurn = Players[0];  
        }  
    }  

    else if (NumPlayers == 4)  
    {  
        if (PlayerTurn == Players[0])  
        {  
            PlayerTurn = Players[1];  
        }  
        else if (PlayerTurn == Players[1])  
        {  
            PlayerTurn = Players[2];  
        }  
        else if (PlayerTurn == Players[2])  
        {  
            PlayerTurn = Players[3];  
        }  
        else if (PlayerTurn == Players[3])  
        {  
            PlayerTurn = Players[0];  
        }  
    }  
}  

}

void EndTurn()
{
if (NumPlayers == 2)
{
if (PlayerTurn == Players[0])
{
PlayerTurn = Players[1];
}
else if (PlayerTurn == Players[1])
{
PlayerTurn = Players[0];
}
}

else if (NumPlayers == 3)  
{  
    if (PlayerTurn == Players[0])  
    {  
        PlayerTurn = Players[1];  
    }  
    else if (PlayerTurn == Players[1])  
    {  
        PlayerTurn = Players[2];  
    }  
    else if (PlayerTurn == Players[2])  
    {  
        PlayerTurn = Players[0];  
    }  
}  

else if (NumPlayers == 4)  
{  
    if (PlayerTurn == Players[0])  
    {  
        PlayerTurn = Players[1];  
    }  
    else if (PlayerTurn == Players[1])  
    {  
        PlayerTurn = Players[2];  
    }  
    else if (PlayerTurn == Players[2])  
    {  
        PlayerTurn = Players[3];  
    }  
    else if (PlayerTurn == Players[3])  
    {  
        PlayerTurn = Players[0];  
    }  
}  

}

void Start()
{
cout << "\n\n";
cout << "Welcome to Farkle!\n\n";
cout << "How many players are playing?\n";
cin >> NumPlayers;
cout << "\n";

for (int i = 0; i < NumPlayers; i++)  
{  
    cout << "What is the name of player " << i + 1 << "?\n";  
    cin >> Players[i];  
    cout << "\n";  
}  

}

void Quit()
{
cout << "\n";
cout << "Thank you for playing Farkle.\n";
cout << "Goodbye.\n";
}

class Player
{
private:
int PlayerScore;
int PlayerTempScore;
int PlayerTurn;
int NumDiceKept;
int Dice[6];
string PlayerName;

public:
Player(string PlayerName, int PlayerScore, int PlayerTempScore, int PlayerTurn, int NumDiceKept, int Dice1, int Dice2, int Dice3, int Dice4, int Dice5, int Dice6)
{
PlayerName = PlayerName;
PlayerScore = PlayerScore;
PlayerTempScore = PlayerTempScore;
PlayerTurn = PlayerTurn;
NumDiceKept = NumDiceKept;
Dice1 = Dice1;
Dice2 = Dice2;
Dice3 = Dice3;
Dice4 = Dice4;
Dice5 = Dice5;
Dice6 = Dice6;
}
};

void setname(string PlayerName)
{
for (int i = 0; i < NumPlayers; i++)
{
Players[i] = PlayerName;
}
};

void rolldice()
{
if (NumDiceKept == 0)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
die3 = rand() % 6 + 1;
die4 = rand() % 6 + 1;
die5 = rand() % 6 + 1;
die6 = rand() % 6 + 1;
}

else if (NumDiceKept == 1)  
{  
    die1 = rand() % 6 + 1;  
    die2 = rand() % 6 + 1;  
    die3 = rand() % 6 + 1;  
    die4 = rand() % 6 + 1;  
    die5 = rand() % 6 + 1;  
}  

else if (NumDiceKept == 2)  
{  
    die1 = rand() % 6 + 1;  
    die2 = rand() % 6 + 1;  
    die3 = rand() % 6 + 1;  
    die4 = rand() % 6 + 1;  
}  

else if (NumDiceKept == 3)  
{  
    die1 = rand() % 6 + 1;  
    die2 = rand() % 6 + 1;  
    die3 = rand() % 6 + 1;  
}  

else if (NumDiceKept == 4)  
{  
    die1 = rand() % 6 + 1;  
    die2 = rand() % 6 + 1;  
}  

else if (NumDiceKept == 5)  
{  
    die1 = rand() % 6 + 1;  
}  

};

void PlayerInputMethod()
{
cout << "Player " << PlayerTurn << ", it is your turn.\n";
cout << "You have " << PlayerTempScore << " points this turn.\n";
cout << "You have " << PlayerScore << " points total on the score board.\n";
cout << "You have " << 6 - NumDiceKept << " dice left.\n";

getline(cin, PlayerInput);  

cout << Players[i] << "'s Options: " << "\n\n";  

cout << "1. Roll Dice | 2. Keep Dice | 3. End Turn | 4. Quit Game\n\n";  

if (PlayerInput == "Roll Dice" || "roll dice")  
{  
    RollDice();  
}  

else if (PlayerInput == "Keep Dice" || "keep dice")  
{  
    KeepDice();  
}  

else if (PlayerInput == "End Turn" || "end turn")  
{  
    EndTurn();  
}  

else if (PlayerInput == "Quit Game" || "quit game")  
{  
    Quit();  
}  

else  
{  
    cout << "Invalid input. Please try again.\n\n";  
    PlayerInput;  
}  

};

int main() // This is the intro to the game and the list of rules to follow.
{
// This while loop will continue to ask for the amount of players until a correct value is provided.
while (NumPlayers < 2 || NumPlayers > 10)
{
cout << "\n\n";
cout << "Welcome to Farkle!\n";
cout << "\n\n";

    cout << "How many players are playing?\n";  
    cin >> NumPlayers;  
    cout << "\n";  

    if (NumPlayers < 2 || NumPlayers > 10)  
    {  
        cout << "Invalid input. Please try again.\n\n";  
    }  

    for (int i = 0; i < NumPlayers; i++)  
    {  
        cout << "What is the name of player " << i + 1 << "?\n";  
        cin >> Players[i];  
        cout << "\n";  
    }  

    for (int i = 0; i < NumPlayers; i++)  
    {  
        cout << "Player " << i + 1 << " is " << Players[i] << ".\n";  
    }  

    DisplayRules();  
    cout << "Would you like to play Farkle?";  
    cin >> BeginGame;  
    cout << "\n";  

    if (BeginGame == "Yes" || BeginGame == "yes")  
    {  
        cout << "Let's play!\n\n";  
        break;  
    }  

    else if (BeginGame == "No" || BeginGame == "no")  
    {  
        cout << "Thank you for playing Farkle.\n";  
        cout << "Goodbye.\n";  
        break;  
    }  

    else  
    {  
        cout << "Invalid input. Please try again.\n\n";  
    }  
    Start();  
    PlayerInputMethod();  
}  
return 0;  

}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,883 questions
{count} votes

Accepted answer
  1. Viorel 120.5K Reputation points
    2023-01-05T20:20:34.827+00:00

    Try to replace string PlayerInput = 0; with string PlayerInput; and continue debugging.

    Also consider restoring the original ucrtbased.dll, which is probably more reliable.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Edward Williams 21 Reputation points
    2023-01-05T11:52:59.04+00:00

    This is the code. I was working on it for a previous class and it was working for the most part when it was coded manually, but when we got into a week we were learning classes, I messed it up somehow while trying to transition everything to a class. Any help would be very much appreciated.

    /*
    Edward Anthony Williams
    IT-312
    27 November 2022
    */

    include <random> // random is needed to generate random numbers for the dice.

    include <iostream>

    include <string>

    include <cstdlib>

    include <ctime>

    using namespace std;
    using std::string;
    using std::cin;
    using std::cout;

    // This is the set of classes that will be used in the program.
    class Winner;
    class Player;

    int i;
    // These variables will represent the dice for the game of Farkle.
    int die1;
    int die2;
    int die3;
    int die4;
    int die5;
    int die6;

    // This variable is used to track the input of the user to decide which dice to keep.
    int KeepDiceChoice = 0;

    // This string variable is used to track whether the dice selected are a set.
    string DiceSet = "";

    // These variables are used to track the number of dice in a set that are kept.
    int DiceSet1 = 0;
    int DiceSet2 = 0;
    int DiceSet3 = 0;
    int DiceSet4 = 0;
    int DiceSet5 = 0;
    int DiceSet6 = 0;

    // These variables are used to track the dice kept that were not in a set.
    int Dice1 = 0;
    int Dice2 = 0;
    int Dice3 = 0;
    int Dice4 = 0;
    int Dice5 = 0;
    int Dice6 = 0;

    // These variables are used to track the number of dice in a set that are kept.
    string KeepDice1;
    string KeepDice2;
    string KeepDice3;
    string KeepDice4;
    string KeepDice5;
    string KeepDice6;

    // this is the int variable used to select the amount of players playing the game. For this game there will be a limiter where only 2-4 players can play.
    int NumPlayers = 0;
    string PlayerInput = 0;

    int PlayerTempScore = 0; // This player score keeps the score of the current players turn.
    int PlayerScore[4] = { 0, 0, 0, 0, }; // This array of int variables is to help keep track of the player's points.

    int NumDiceKept = 0; // This variable is used to track the number of dice kept.
    int NumDiceSet = 0; // This integer stores the number of dice in a set.

    string BeginGame = ""; // String to decide whether the game should begin or exit.
    string PlayerTurn = ""; // This array keeps track of whose turn it is.
    string Players[4] = { "Player 1", "Player 2", "Player 3", "Player 4" }; // This array of string variables is used to hold the name of the players.
    string CurrentPlayer = ""; // This variable is used to keep track of the current player.

    void DisplayRules()
    {
    cout << "Welcome to Farkle!" << "\n";
    cout << "The rules for the game of farkle are as follows:" << "\n\n";
    cout << "1. The game is played with six dice." << "\n";
    cout << "2. The player must roll all six dice at least once per turn." << "\n";
    cout << "3. You will have 3 rolls to get a score." << "\n";
    cout << "4. Before a player can score their points for the round they mus initially obtain" << "\n";
    cout << " 1000 points in a single round." << "\n";
    cout << "5. Once the player has obtained 1000 points in a single round they can choose" << "\n";
    cout << " to keep their points or roll again." << "\n";
    cout << "6. Only once you have acquired 1000 points and kept them for the round you got" "\n";
    cout << " them can you choose to keep any points obtained in future rolls." << "\n";
    cout << "7. The first player rolls all six dice at the same time and sets aside any point dice." << "\n";
    cout << "8. A farkle happens when a player rolls the dice and doesn't obtain any point dice in the roll." << "\n";
    cout << "9. If a player rolls a farkle they lose all points they have acquired in the round" << "\n";
    cout << " and the turn passes to the next player." << "\n";
    cout << "10. If a player rolls a farkle and has not acquired 1000 points in the round they lose all points" << "\n";
    cout << " they have acquired in the game." << "\n";
    cout << "11. If a player does not wish to risk losing the points they have obtained in the round," << "\n";
    cout << " they may choose to stop rolling and count their score for the round as long as rule 3 is true." << "\n";
    cout << "12. If during one turn, all six dice become point dice and are set aside, the player must roll all" << "\n";
    cout << " six dice at least one more time, before stopping and keeping the points collected in the round." << "\n";
    cout << "13. It may ocassionally be beneficial to roll some of the point dice with the non-point dice" << "\n";
    cout << " in order to have a higher chance at obtaining a three of a kind. But at least 1 point die" << "\n";
    cout << " must be set aside after each roll." << "\n";
    cout << "14. The player who has reached the score limit of 10,000 is the winner." << "\n\n";
    };

    class Winner
    {
    private:
    void DisplayScore()
    {
    for (int i = 0; i < NumPlayers; i++)
    {
    if (PlayerScore[i] >= 10000)
    {
    cout << Players[i] << " Wins!" << endl;
    exit(0);
    }
    }
    }
    };

    void RollDice() // This function rolls the dice depending on the amount of dice currently kept. Then it displays the dice roll to the player.
    {
    srand(time(NULL));
    for (int i = 0; i < NumDiceKept; i++)
    {
    switch (i)
    {
    case 0:
    die1 = rand() % 6 + 1;
    cout << "Die 1: " << die1 << "\n";
    break;
    case 1:
    die2 = rand() % 6 + 1;
    cout << "Die 2: " << die2 << "\n";
    break;
    case 2:
    die3 = rand() % 6 + 1;
    cout << "Die 3: " << die3 << "\n";
    break;
    case 3:
    die4 = rand() % 6 + 1;
    cout << "Die 4: " << die4 << "\n";
    break;
    case 4:
    die5 = rand() % 6 + 1;
    cout << "Die 5: " << die5 << "\n";
    break;
    case 5:
    die6 = rand() % 6 + 1;
    cout << "Die 6: " << die6 << "\n";
    break;
    }
    }
    }

    void KeepDice() // This function facilitates the process of keeping the dice the user selects.
    {
    if (die1 == 1 || die1 == 5 || die2 == 1 || die2 == 5 || die3 == 1 || die3 == 5 || die4 == 1 || die4 == 5 || die5 == 1 || die5 == 5 || die6 == 1 || die6 == 5)
    {
    cout << "What dice would you like to keep?\n";
    cout << "\n";
    cout << "Is this die part of a set?\n";
    getline(cin, DiceSet);

        if (DiceSet == "Yes" || DiceSet == "yes")  
        {  
            cout << "Enter the dice that are in the set, one at a time.\n\n";  
            cin >> DiceSet1;  
            cout << "\n";  
            cin >> DiceSet2;  
            cout << "\n";  
            cin >> DiceSet3;  
            cout << "\n";  
    
            if (die1 == 1 && die2 == 1 && die3 == 1)  
            {  
                PlayerTempScore = PlayerTempScore + 1000;  
                NumDiceKept = (NumDiceKept + 3);  
            }  
            if (die1 == 2 && die2 == 2 && die3 == 2)  
            {  
                PlayerTempScore = PlayerTempScore + 200;  
                NumDiceKept = (NumDiceKept + 3);  
            }  
            else if (die1 == 3 && die2 == 3 && die3 == 3)  
            {  
                PlayerTempScore = PlayerTempScore + 300;  
                NumDiceKept = (NumDiceKept + 3);  
            }  
            else if (die1 == 4 && die2 == 4 && die3 == 4)  
            {  
                PlayerTempScore = PlayerTempScore + 400;  
                NumDiceKept = (NumDiceKept + 3);  
            }  
            else if (die1 == 5 && die2 == 5 && die3 == 5)  
            {  
                PlayerTempScore = PlayerTempScore + 500;  
                NumDiceKept = (NumDiceKept + 3);  
            }  
            else if (die1 == 6 && die2 == 6 && die3 == 6)  
            {  
                PlayerTempScore = PlayerTempScore + 600;  
                NumDiceKept = (NumDiceKept + 3);  
            }  
    
    
        }  
    
        else if (DiceSet == "No" || DiceSet == "no")  
        {  
            cout << "What dice would you like to keep?\n\n";  
            cin >> KeepDiceChoice;  
            cout << "\n";  
    
            if (KeepDiceChoice == 1)  
            {  
                if (Dice1 == 1 || Dice1 == 5)  
                {  
                    cout << "Die 1: " << die1 << "\n";  
                    cout << "Keep this die?\n";  
                    getline(cin, KeepDice1);  
                    if (KeepDice1 == "Yes" || KeepDice1 == "yes")  
                    {  
                        NumDiceKept = (NumDiceKept + 1);  
                        if (die1 == 1)  
                        {  
                            PlayerTempScore = PlayerTempScore + 100;  
                        }  
                        else if (die1 == 5)  
                        {  
                            PlayerTempScore = PlayerTempScore + 50;  
                        }  
                    }  
                }  
            }  
    
            else if (KeepDiceChoice == 2)  
            {  
                if (Dice2 == 1 || Dice2 == 5)  
                {  
                    cout << "Die 2: " << die2 << "\n";  
                    cout << "Keep this die?\n";  
                    getline(cin, KeepDice2);  
    
                    if (KeepDice2 == "Yes" || KeepDice2 == "yes")  
                    {  
                        NumDiceKept = (NumDiceKept + 1);  
                        if (die1 == 1)  
                        {  
                            PlayerTempScore = PlayerTempScore + 100;  
                        }  
                        else if (die1 == 5)  
                        {  
                            PlayerTempScore = PlayerTempScore + 50;  
                        }  
                    }  
                }  
            }  
    
            else if (KeepDiceChoice == 3)  
            {  
                if (Dice3 == 1 || Dice3 == 5)  
                {  
                    cout << "Die 3: " << die3 << "\n";  
                    cout << "Keep this die?\n";  
                    getline(cin, KeepDice3);  
    
                    if (KeepDice3 == "Yes" || KeepDice3 == "yes")  
                    {  
                        NumDiceKept = (NumDiceKept + 1);  
                        if (die3 == 1)  
                        {  
                            PlayerTempScore = PlayerTempScore + 100;  
                        }  
                        else if (die3 == 5)  
                        {  
                            PlayerTempScore = PlayerTempScore + 50;  
                        }  
                    }  
                }  
            }  
    
            else if (KeepDiceChoice == 4)  
            {  
                if (Dice4 == 1 || Dice4 == 5)  
                {  
                    cout << "Die 4: " << die4 << "\n";  
                    cout << "Keep this die?\n";  
                    getline(cin, KeepDice4);  
    
                    if (KeepDice4 == "Yes" || KeepDice4 == "yes")  
                    {  
                        NumDiceKept = (NumDiceKept + 1);  
                        if (die4 == 1)  
                        {  
                            PlayerTempScore = PlayerTempScore + 100;  
                        }  
                        else if (die4 == 5)  
                        {  
                            PlayerTempScore = PlayerTempScore + 50;  
                        }  
                    }  
                }  
            }  
    
            else if (KeepDiceChoice == 5)  
            {  
                if (Dice5 == 1 || Dice5 == 5)  
                {  
                    cout << "Die 5: " << die5 << "\n";  
                    cout << "Keep this die?\n";  
                    getline(cin, KeepDice5);  
    
                    if (KeepDice5 == "Yes" || KeepDice5 == "yes")  
                    {  
                        NumDiceKept = (NumDiceKept + 1);  
                        if (die5 == 1)  
                        {  
                            PlayerTempScore = PlayerTempScore + 100;  
                        }  
                        else if (die5 == 5)  
                        {  
                            PlayerTempScore = PlayerTempScore + 50;  
                        }  
                    }  
                }  
            }  
    
            else if (KeepDiceChoice == 6)  
            {  
                if (Dice6 == 1 || Dice6 == 5)  
                {  
                    cout << "Die 6: " << die6 << "\n";  
                    cout << "Keep this die?\n";  
                    getline(cin, KeepDice6);  
    
                    if (KeepDice6 == "Yes" || KeepDice6 == "yes")  
                    {  
                        NumDiceKept = (NumDiceKept + 1);  
                        if (die6 == 1)  
                        {  
                            PlayerTempScore = PlayerTempScore + 100;  
                        }  
                        else if (die6 == 5)  
                        {  
                            PlayerTempScore = PlayerTempScore + 50;  
                        }  
                    }  
                }  
            }  
        }  
        else  
        {  
            cout << "Invalid input. Please try again.\n\n";  
            KeepDice();  
        }  
    }  
    
    else  
    {  
        cout << "Farkle!!!";  
    
        if (NumPlayers == 2)  
        {  
            if (PlayerTurn == Players[0])  
            {  
                PlayerTurn = Players[1];  
            }  
            else if (PlayerTurn == Players[1])  
            {  
                PlayerTurn = Players[0];  
            }  
        }  
    
        else if (NumPlayers == 3)  
        {  
            if (PlayerTurn == Players[0])  
            {  
                PlayerTurn = Players[1];  
            }  
            else if (PlayerTurn == Players[1])  
            {  
                PlayerTurn = Players[2];  
            }  
            else if (PlayerTurn == Players[2])  
            {  
                PlayerTurn = Players[0];  
            }  
        }  
    
        else if (NumPlayers == 4)  
        {  
            if (PlayerTurn == Players[0])  
            {  
                PlayerTurn = Players[1];  
            }  
            else if (PlayerTurn == Players[1])  
            {  
                PlayerTurn = Players[2];  
            }  
            else if (PlayerTurn == Players[2])  
            {  
                PlayerTurn = Players[3];  
            }  
            else if (PlayerTurn == Players[3])  
            {  
                PlayerTurn = Players[0];  
            }  
        }  
    }  
    

    }

    void EndTurn()
    {
    if (NumPlayers == 2)
    {
    if (PlayerTurn == Players[0])
    {
    PlayerTurn = Players[1];
    }
    else if (PlayerTurn == Players[1])
    {
    PlayerTurn = Players[0];
    }
    }

    else if (NumPlayers == 3)  
    {  
        if (PlayerTurn == Players[0])  
        {  
            PlayerTurn = Players[1];  
        }  
        else if (PlayerTurn == Players[1])  
        {  
            PlayerTurn = Players[2];  
        }  
        else if (PlayerTurn == Players[2])  
        {  
            PlayerTurn = Players[0];  
        }  
    }  
    
    else if (NumPlayers == 4)  
    {  
        if (PlayerTurn == Players[0])  
        {  
            PlayerTurn = Players[1];  
        }  
        else if (PlayerTurn == Players[1])  
        {  
            PlayerTurn = Players[2];  
        }  
        else if (PlayerTurn == Players[2])  
        {  
            PlayerTurn = Players[3];  
        }  
        else if (PlayerTurn == Players[3])  
        {  
            PlayerTurn = Players[0];  
        }  
    }  
    

    }

    void Start()
    {
    cout << "\n\n";
    cout << "Welcome to Farkle!\n\n";
    cout << "How many players are playing?\n";
    cin >> NumPlayers;
    cout << "\n";

    for (int i = 0; i < NumPlayers; i++)  
    {  
        cout << "What is the name of player " << i + 1 << "?\n";  
        cin >> Players[i];  
        cout << "\n";  
    }  
    

    }

    void Quit()
    {
    cout << "\n";
    cout << "Thank you for playing Farkle.\n";
    cout << "Goodbye.\n";
    }

    class Player
    {
    private:
    int PlayerScore;
    int PlayerTempScore;
    int PlayerTurn;
    int NumDiceKept;
    int Dice[6];
    string PlayerName;

    public:
    Player(string PlayerName, int PlayerScore, int PlayerTempScore, int PlayerTurn, int NumDiceKept, int Dice1, int Dice2, int Dice3, int Dice4, int Dice5, int Dice6)
    {
    PlayerName = PlayerName;
    PlayerScore = PlayerScore;
    PlayerTempScore = PlayerTempScore;
    PlayerTurn = PlayerTurn;
    NumDiceKept = NumDiceKept;
    Dice1 = Dice1;
    Dice2 = Dice2;
    Dice3 = Dice3;
    Dice4 = Dice4;
    Dice5 = Dice5;
    Dice6 = Dice6;
    }
    };

    void setname(string PlayerName)
    {
    for (int i = 0; i < NumPlayers; i++)
    {
    Players[i] = PlayerName;
    }
    };

    void rolldice()
    {
    if (NumDiceKept == 0)
    {
    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1;
    die3 = rand() % 6 + 1;
    die4 = rand() % 6 + 1;
    die5 = rand() % 6 + 1;
    die6 = rand() % 6 + 1;
    }

    else if (NumDiceKept == 1)  
    {  
        die1 = rand() % 6 + 1;  
        die2 = rand() % 6 + 1;  
        die3 = rand() % 6 + 1;  
        die4 = rand() % 6 + 1;  
        die5 = rand() % 6 + 1;  
    }  
    
    else if (NumDiceKept == 2)  
    {  
        die1 = rand() % 6 + 1;  
        die2 = rand() % 6 + 1;  
        die3 = rand() % 6 + 1;  
        die4 = rand() % 6 + 1;  
    }  
    
    else if (NumDiceKept == 3)  
    {  
        die1 = rand() % 6 + 1;  
        die2 = rand() % 6 + 1;  
        die3 = rand() % 6 + 1;  
    }  
    
    else if (NumDiceKept == 4)  
    {  
        die1 = rand() % 6 + 1;  
        die2 = rand() % 6 + 1;  
    }  
    
    else if (NumDiceKept == 5)  
    {  
        die1 = rand() % 6 + 1;  
    }  
    

    };

    void PlayerInputMethod()
    {
    cout << "Player " << PlayerTurn << ", it is your turn.\n";
    cout << "You have " << PlayerTempScore << " points this turn.\n";
    cout << "You have " << PlayerScore << " points total on the score board.\n";
    cout << "You have " << 6 - NumDiceKept << " dice left.\n";

    getline(cin, PlayerInput);  
    
    cout << Players[i] << "'s Options: " << "\n\n";  
    
    cout << "1. Roll Dice | 2. Keep Dice | 3. End Turn | 4. Quit Game\n\n";  
    
    if (PlayerInput == "Roll Dice" || "roll dice")  
    {  
        RollDice();  
    }  
    
    else if (PlayerInput == "Keep Dice" || "keep dice")  
    {  
        KeepDice();  
    }  
    
    else if (PlayerInput == "End Turn" || "end turn")  
    {  
        EndTurn();  
    }  
    
    else if (PlayerInput == "Quit Game" || "quit game")  
    {  
        Quit();  
    }  
    
    else  
    {  
        cout << "Invalid input. Please try again.\n\n";  
        PlayerInput;  
    }  
    

    };

    int main() // This is the intro to the game and the list of rules to follow.
    {
    // This while loop will continue to ask for the amount of players until a correct value is provided.
    while (NumPlayers < 2 || NumPlayers > 10)
    {
    cout << "\n\n";
    cout << "Welcome to Farkle!\n";
    cout << "\n\n";

        cout << "How many players are playing?\n";  
        cin >> NumPlayers;  
        cout << "\n";  
    
        if (NumPlayers < 2 || NumPlayers > 10)  
        {  
            cout << "Invalid input. Please try again.\n\n";  
        }  
    
        for (int i = 0; i < NumPlayers; i++)  
        {  
            cout << "What is the name of player " << i + 1 << "?\n";  
            cin >> Players[i];  
            cout << "\n";  
        }  
    
        for (int i = 0; i < NumPlayers; i++)  
        {  
            cout << "Player " << i + 1 << " is " << Players[i] << ".\n";  
        }  
    
        DisplayRules();  
        cout << "Would you like to play Farkle?";  
        cin >> BeginGame;  
        cout << "\n";  
    
        if (BeginGame == "Yes" || BeginGame == "yes")  
        {  
            cout << "Let's play!\n\n";  
            break;  
        }  
    
        else if (BeginGame == "No" || BeginGame == "no")  
        {  
            cout << "Thank you for playing Farkle.\n";  
            cout << "Goodbye.\n";  
            break;  
        }  
    
        else  
        {  
            cout << "Invalid input. Please try again.\n\n";  
        }  
        Start();  
        PlayerInputMethod();  
    }  
    return 0;  
    

    }


  2. Edward Williams 21 Reputation points
    2023-01-07T17:45:08.69+00:00

    Thank you for the help. That worked immediately. I kind of feel like an idiot now seeing how simple of a problem that was to fix. I had thought I changed all the strings and ints when I was switching them around but I apparently missed one.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.