lHello,
my question is that I can not seem to locate where my error is.
The problem is that although the code runs it doesn't run completely, the game stops just when you are about to actually play the game. so the pig dice function runs partly and never gets to pd_turn function. my code is below.
Any help would be appreciated.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
void pigDice();
bool rollOff;
bool winner;
int rollDie();
int pd_turn(string name);
int player;
void save();
void Menu();
int main()
{
Menu();
return 0;
}
void Menu() {
char choice = 'z';
while (choice != 'q') {
cout << "Menu\n"
<< "a) Pig Dice Game\n"
<< "b) Roll Die\n"
<< "c) Game 3\n"
<< "d) Stats menu\n"
<< "q) quit\n"
<< "Please enter a menu selection: ";
cin >> choice;
switch (choice)
{
case'a':
case'A':
pigDice();
int pd_turn(string player);
int rollDie();
break;
case'b':
case'B':
cout << "\nYou rolled a " << rollDie() << ".\n\n";
int rollDie();
break;
case'c':
case'C':
break;
case'd':
case'D':
break;
case'q':
case'Q':
cout << "quitting game" << ".\n\n";
choice ='q';
break;
default:
cout << "Incorrect input please try again.\n";
}//end of switch
} //end of while
}
void pigDice() //function to control the game play for pigDice
{
int player1_score, player2_score, player;
char choice = 'z';
string player1, player2;
bool rollOff;
//get name of players
cout << "Player1 enter your name: ";
cin >> player1;
cout << "Player2 enter your name: ";
cin >> player2;
//players roll to determine the order of play. Place in a loop in case of tie.
while (choice != 'n')
{
cout << "\nDetermining the order of play. Player1 roll the dice. \n";
system("pause");
player1_score = rollDie();
cout << "\nPlayer1 you rolled a " << player1_score << ".\n";
cout << "\nPlayer2 roll the dice.\n";
system("pause");
player2_score = rollDie();
cout << "\nPlayer2 you rolled a " << player2_score << ".\n";
//establish who plays 1st and who plays 2nd
if (player1_score < player2_score) {
player1_score = 0;
player2_score = 1;
cout << player1 << " you play first. " << player2 << " you go second.\n";
choice = 'n'; // terminate this loop
}
elseif (player2_score < player1_score) {
player2_score = 0;
player1_score = 1;
cout << player2 << " you play first. " << player1 << " you go second.\n";
choice = 'n'; //terminate this loop
}
else
cout << "\nIt's a tie. You both roll a " << player1_score << ".\n"
<< "Please try again.\n";
}//end of while
//first player to play plays - play function
if (player1_score = 0) {
player = 1;
player2_score = 0;
player1_score = pd_turn(player1);
if (player1_score >= 50)
rollOff = true;
}
else {
player = 2;
player1_score = 0;
player2_score = pd_turn(player2);
if (player2_score >= 50)
rollOff = true;
if (player1_score == 0)
player1_score = pd_turn(player1);
else
player2_score = pd_turn(player2);
}
//check if score is >50. if it is then we have a roll off
if (player1_score == 0) {
player = 1;
player2_score = 0;
player1_score = pd_turn(player1);
if (player1_score >= 50)
rollOff = true;
}
else {
player = 2;
player1_score = 0;
player2_score = pd_turn(player2);
if (player2_score >= 50)
rollOff = true;
}
//if we have a roll off, second player must roll until win or lose
if (rollOff)
if (player == 1) {
cout << "\nWe have a Roll off! Player" << player2
<< "it is your turn. you must score more than" << player1_score << "to win.\n";
system("pause");
player2_score = pd_turn(player2);
if (player2_score <= player1_score) {
cout << "\nSorry" << player2 << "you did not get enough points. You lose!\n";
save();
return;
}
else {
cout << "'nSorry" << player1 << " you lose!" << player2 << " exceeded your points.\n";
save();
return;
}
}
elseif
(player == 2) {
cout << "\nWe have a Roll off! Player" << player1
<< "it is your turn. you must score more than" << player2_score << "to win.\n";
system("pause");
player1_score = pd_turn(player1);
if (player1_score <= player2_score) {
cout << "\nSorry" << player1 << "you did not get enough points. You lose!\n";
save();
return;
}
else {
cout << "\nSorry" << player2 << " you lose!" << player1 << " exceeded your points.\n";
save();
return;
}
}
else
cout << "\nError. Invalid player.\n";
//if we do not have a roll off, loop,
elsewhile (!winner)
{
if (player == 1) {
player = 2;
cout << "\n" << player2 << "your game score is " << player2_score << ".\n";
player2_score = player2_score + pd_turn(player2);
if (player2_score >= 50) {
cout << "\n" << player2 << "you win! Your score is " << player2_score << ".\n";
winner = true;
save();
}
}
else
{
player = 1;
cout << "\n" << player1 << " your game score is" << player1_score << ".\n";
player1_score = player1_score + pd_turn(player1);
if (player1_score >= 50) {
cout << "\n" << player1 << "you win! Your score is " << player1_score << ".\n";
winner = true;
save();
}
}
}//end of while
}
int rollDie()
{
int die;
srand(time(0));
die = rand() % 6 + 1;
return die;
}
int pd_turn(string player)
{
int score = 0, die =rollDie();
char choice = 'z';
system("pause");
system("cls");
cout << "\n\n" << player << " it is your turn.\n\nYou may roll the dice until you choose to stop.\n"
<< "Once you choose to stop your score for this turn\nwill be added to your overall score. "
<< "If you roll a 1 \nthen you lose your turn and all the points for this turn.\n\n";
while (choice != 'q');
{
cout << "\nrollDie();.\n";
system("pause");
die = rollDie();
cout << "\nYou rolled a" << rollDie << ".\n";
if (die == 1) {
cout << "\nSorry, you rolled a " << die << " and so you lose your turn.\nYou get 0 points this turn.\n";
score = rollDie();
return die;
}
else {
score = die;
cout << "\nYour current points for this turn is: " << score << endl
<< "\nWould you like to roll again or quit? Enter y to continue and n to quit: ";
cin >> choice;
system("cls");
}
}//end of while
return score;
}
void save()
{
}
//end of pigDice game
Your program does enter the function pd_turn. However, the reason it stops is because of a misplaced semicolon. Read the comment below.
1 2 3 4 5 6 7
cout << "\n\n" << player << " it is your turn.\n\nYou may roll the dice until you choose to stop.\n"
<< "Once you choose to stop your score for this turn\nwill be added to your overall score. "
<< "If you roll a 1 \nthen you lose your turn and all the points for this turn.\n\n";
while (choice != 'q'); // There should not be a semicolon here. Remove it.
{
cout << "\nrollDie();.\n";