I am writing a game program for a dice game. You want to get 100 points. You roll until you decide to hold, and the sum of your rolls is added to your total score. If you roll a 1 though, your turn is over and you get 0 points for that turn.
My problem is that when the user (me) rolls a 1, it appears that the computer rolls a 1 right after me. For example I get 0 points for my turn, without fail after me the computer gets 0 points also. If I hold, it acts how I want it to (the computer gets points but sometimes gets 0). There are 3 functions besides main. One for the human turn, the computer turn, and to simulate a dice roll. I'm pretty stumped as to how these functions are affecting each other. If anyone has any suggestions or hints as to what the problem is please let me know, thanks.

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

int humanTurn(int humanTotalScore); // human turn function
int computerTurn (int computerTotalScore); // computer turn function
int diceRoll (); // dice roll function

int main ()
{
	int humanTotalScore = 0;
	int computerTotalScore = 0;

	cout << "Are you ready to play?  Good." << endl;

	while ((computerTotalScore < 100) && (humanTotalScore < 100))
	{
		humanTotalScore = humanTurn (humanTotalScore);
		computerTotalScore = computerTurn(computerTotalScore);
	}

	if (humanTotalScore >= 100) // human wins
	{
		cout << "Good job, you won." << endl;
	}
	else
	{
		cout << "You lost, better luck next time." << endl; // computer wins
	}
	
	
		
}

int diceRoll()
{
	int x;
	srand (time(NULL)); // uses a seed based on the time
	x=((rand()%6)+1);
	return x;
}

int humanTurn (int humanTotalScore)
{
	int score = 0;
	int roll;
	char rollAgain;

	cout << "Your total score is " << humanTotalScore << "." << endl;
	cout << "Your score for this turn is " << score << "." << endl;
	cout << "Enter r to roll again or h to hold." << endl;
	cin >> rollAgain;

	while ((rollAgain == 'r') || (rollAgain == 'R'))
	{
		roll = diceRoll();

		if (roll == 1)
		{
			cout << "You rolled a 1, your turn is over and you gain 0 points." << endl;
			cout << endl;
			return humanTotalScore; // humanTotalScore is returned without adding anything to it
			
		}
		else 
		{
			score = score + roll;
			cout << "You rolled a " << roll << " and your score for this turn is " << endl;
			cout << score << ".  Enter r to roll again or h to hold." << endl;
			cin >> rollAgain;
		}
	}
    while ((rollAgain == 'h') || (rollAgain == 'H'))
	{
		humanTotalScore = humanTotalScore + score; // humanTotalScore is returned after adding the turn score to it
		return humanTotalScore;
	}
}

int computerTurn (int computerTotalScore)
{
	int score = 0;
	int roll;

	roll = diceRoll();

	while (score < 20)
	{
		if ((roll == 1))
		{
			cout << "Computer gained 0 points this turn and has a total of " << endl;
			cout << computerTotalScore << " points." << endl;
			cout << endl;
			return computerTotalScore;
		}
		else 
		{
			score = score + roll;
		}
	}
	
	{
		computerTotalScore = computerTotalScore + score;
		cout << "Computer gained " << score << " points this turn." << endl;
		cout << "Computer has a total of " << computerTotalScore << " points." << endl;
		cout << endl;
	}
	return computerTotalScore;
}

I didn't compile and run your program yet, but try putting the srand() call out in main() so it's only done once.

Wow yeah that fixed it. Thanks, now I need to figure out why that works.

It's just magic. The little gnomes inside your CPU like it that way.

It's seeding it based upon the clock time. If you seed it once it will go through it's predefined sequence (see Narue's tutorial) and generate a series of numbers but what you were doing was (re)seeding it with each dice roll and if they happened too close in time you'd end up starting the sequence over again with (roughly) the same seed.

Wow yeah that fixed it. Thanks, now I need to figure out why that works.

I am probably missing something but your while loop on line 17

is this the logic you want?

if you hold at say 70 and the computer rolls a 1
doesn't that mean that you have a total human score of 70 and the computer total is 0 and that you should win.

But the while loop won't exit.

also
line 74:
while()
with a single return and no way to change the exit condition
is a bad choice of block
should be if()

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.