problem returning a varable

I am working on a game of pig. Just the human player for now. whenever I try to return the player's score at the end of the turn it comes back as zero. I can not figure out why.

CODE_______________________________________________

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

void greeting();
int player(int playerScore, int pRound);

int main()
{
int playerScore;
playerScore = 0;
int pRound;
int cRound;
int comScore = 0;
int startingPlayer;
greeting();

do
{
player(playerScore, pRound);
cout << "**** Player score " << playerScore << "\n\n\n";
}
while ((playerScore < 100) && (comScore < 100));
}
void greeting()
{
cout << "The goal of Pig is to reach 100 points or more before the other player does.\n";
cout << "Each time you roll the die points are added to your turn total.";
cout << "If you roll a 2-6, then you can choose to roll again or hold ";
cout << "If you hold, then the points are added to your score.\n";
cout << "But if you roll a 1, then all your points for that turn are lost!\n\n";
}

int player(int playerScore, int pRound)
{
int choice = 1;
int tempScore = playerScore;
pRound = 0;
srand (time(NULL));
cout << "If You would like to hold enter 1\n If you would like to roll enter 2\n";
cin >> choice;
while (choice == 2 && pRound != 1)
{
pRound = rand()%6 + 1;
cout << "You rolled a "<< pRound << endl;
if (pRound == 1)
{
tempScore = 0;
cout << "Since you rolled a 1, your turn is over and all points earned this round have entered the void!\n";
choice = 1;
}
if (pRound != 1)
{
tempScore = pRound + tempScore;
cout << "\nYour total for this round is " << tempScore << endl;
cout << "If You would like to hold enter 1\n If you would like to roll enter 2\n";
cin >> choice;
}
}
playerScore = tempScore + playerScore;
cout << "Your score for this turn is " << playerScore<< endl;
return playerScore;
}
whenever I try to return the player's score at the end of the turn it comes back as zero.

Actually, your code ignores the value returned by the function, there's no way of telling what was returned.

See the function call inside the do-loop in main().
1
2
3
4
do
{
    player(playerScore, pRound); // value returned is ignored.


You never set the playerScore to anything besides 0.
1
2
    player(playerScore, pRound); // returns an integer that is used nowhere.
    // this doesn't change the playerScore variable declared in the main() function. 


I would also set the random number seed in the main function so that it doesn't set it to time(NULL) every time the function is called. You only need it once.
Topic archived. No new replies allowed.