Dice Game Score Accumulator

closed account (E8AXSL3A)

I am writing a C++ program for two games, the one I am currently working in on the code below is called PIG. It is a game where the user plays against the computer and the first one to reach a score of 100 wins. The only catch is that if the player or computer rolls a 1 (PIG) they receive no points for that turn and it ends their turn.

I am having difficulty with creating an accumulator of the dice roll to add to the user's score. Any help, ideas or suggestion? As always its greatly appreciated and thank you for your time!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<Windows.h>
using namespace std;

//Function Prototypes
int getRandomNum();

int main()
{

	//Declare Constants And Variables
	char playGame = 'X';
	char gameChoice = 'X';
	char playerChoice = 'X';
	int randInteger = 0;
	int roll = 0;
	int score = 0;


	//Program Title
	cout << "## IT'S GAME TIME ##\n" << endl;

	//User Chooses To Play Or Not To Play
	cout << "Would You Like To Play A Game? (Y/N): " << endl;
	cin >> playGame;
	playGame = (toupper(playGame));

	if (playGame == 'Y')
	{
		cout << "Fantastic! What Would You Like To Play Today?\n  P for PIG\n  B for BLACKJACK\n\n  E to EXIT\n" << endl;
		cin >> gameChoice;
		gameChoice = (toupper(gameChoice));
	}
	else
	{
		cout << "We Can Play Another Day... Goodbye!" << endl;
	}
	//End If

	//Game Selection
	if (gameChoice == 'P')
	{
		cout << "\WOOHOO!! You Have Chosen To Play Pig! Let's Get Started!" << endl;
		system("pause");
		system("cls");
		cout << "PIG\n\n";
		cout << "Directions:\n\nTwo players race to reach 100 points.\n Each turn, a player repeatedly rolls a die until either a 1 (Pig) is rolled or the player chooses to hold.\n If a player rolls a pig, they score 0 points for that turn.\n If a player holds, they score the sum of the rolls for that turn.\n\n     LET'S GET STARTED!!\n" << endl;
	}
	else if (gameChoice == 'B')
	{
		cout << "\WOOHOO!! You Have Chosen To Play Blackjack! Let's Get Started!" << endl;
		system("pause");
		system("cls");
		cout << "BLACKJACK\n\n";
		cout << "Directions:\n\nThe game is played using a 10 sided dice.\n The dice numbers are 0-9.\n Each player takes a turn rolling two die adding up the numbers (rolling a 0 adds 10 points to the total).\n After the initial roll of two dice, a player may choose to roll an additional die as many times as they wish, adding the resulting number from each roll to their total.\n If the total exceeds 21, the player goes bust and loses that hand even if the computer goes bust too.\n" << endl;
	}
	else if (gameChoice == 'E')
	{
		cout << "Thanks For Playing, Let's Play Again Soon!" << endl;
		system("pause");
		return 0;
	}
	//End Opening Game Selection Statement

	//Start Playing Pig
	while (gameChoice == 'P')
	{
		cout << "It Is Your Turn: Roll The Dice (R) or Hold Your Turn (H):";
		cin >> playerChoice;
		playerChoice = (toupper(playerChoice));

		if (playerChoice == 'R')
		{
				cout << "I Rolled a " << getRandomNum << endl;
				score += roll;
				cout << "My Score is: " << score << endl;
		}
			if (roll == 1)
			{
				cout << "I Rolled A PIG! Ending Player Turn." << endl;
				break;
			}
		if (playerChoice == 'H')
			{
			break;
			}
	}





	system("pause");
	return 0;

}
//End Of Main




//*****Function Definitions******

//Get Random Number Function
int getRandomNum()
{
	int randInteger;
	srand(static_cast<int>(time(0)));
	randInteger = (int)(1 + rand() % (6 - 1 + 1));
	
	return randInteger;
}
//End of Get Random Number Function 
closed account (D80DSL3A)
I think the problem is at line 78
 
cout << "I Rolled a " << getRandomNum << endl;

Try this instead:
1
2
roll = getRandomNum();
cout << "I Rolled a " << roll << endl;
Last edited on
Topic archived. No new replies allowed.