Beginner Dice Game

Hi, i am making a dice game where you roll 2 dices, and add up the totals. If its even, player 1 gets a point, odd, player two. However, i am looking for some advise. I need it to keep track of how many wins each player has and display the winner at the end. How would i go about doing that? I tired somethings in the game function but it does not seem to be working. thanks

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
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;


int Dice (int dice1, int dice2, int sum)
{
      int even;
      int odd;
      int eventotal=0;
dice1 = rand() % (6)+1;
dice2 = rand() % (6)+1;
cout << "First Dice    = " << dice1 <<  endl;
cout << "Second Dice   = " << dice2 << endl;
sum=dice1+dice2;
cout << "The Sum is: " << sum << endl<< endl;

    
    
return (dice1, dice2, sum);
}




int rules()
{ 
int main();
system("cls");
cout << " \nThe Rules Are Quite Simple. We Roll Two Dice.\n    -If the Sum is Even, Player 1 Gets a Point\n    -If the Sum is Odd, Player 2 Gets a Point\n";
cout <<endl;
system("pause");
system("cls");
main ();
}



int game()
{ 
    int times;
    int dice1;
    int dice2;
    int sum;
  int even;
  int odd;
  int eventotal=0;
    system("cls");
    cout << "\n  How Many Times Would You Like to Roll?\n";
    cout << "      Rolls: ";
    cin >> times;
    system("cls");
    while (times>0)
{   
    cout << "Rolls Left :" << times << endl;
           if (sum % 2 == 0)
           {even++;          
               if (even==1)
               {eve n=even+1;
               eventotal=even;}
               else
               {even=even;}}
       else 
             {odd++;}
                
    cout << "Player 1 Points: " << eventotal <<endl;
    cout << "Player 2 Points: " << odd << endl << endl;
    Dice (dice1, dice2, sum);
    times--;}
        
}

     


int main()
{
    system ("color 97");
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    int key;
    cout << "** Are You Feeling Lucky??? **\n\n";
cout << "Menu\n   Press 1 for Rules\n   Press Any Key to Play\n" ;
cout << "\nWhat is Your Choice: ";
cin >> key;
if (key==1)
   {rules();}
else
   {game();}
  system("pause");
}
You shouldn't call main. Is that even allowed? Also you need to return an integer for the rules function.

To keep track of wins just add two global variables. Increment them when the player or players win. Then output the number of wins.

You have a space in line 60 that shouldn't be there. You could just use an increment there.
Line 61 There is no point in that. You don't assign a variable to itself without modify the variable.

From the dice function. You can't return 3 variables.
Last edited on
thanks for your response, i have a few questions tho. Why can't i call main? i used it to return to the home screen again.

i tried to add 1 to the eventotal but i doesn't accumulate for some reason and i dont know why.

and why can i not return 3 variables for dice?

this is what i tried

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
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int eventotal=0;

int Dice (int dice1, int dice2, int sum)
{
      int even;
      int odd;
      int eventotal=0;
dice1 = rand() % (6)+1;
dice2 = rand() % (6)+1;
cout << "First Dice    = " << dice1 <<  endl;
cout << "Second Dice   = " << dice2 << endl;
sum=dice1+dice2;
cout << "The Sum is: " << sum << endl<< endl;
      
    
return (dice1, dice2, sum);
}




int rules()
{ 
int main();
system("cls");
cout << " The Rules Are Quite Simple. We Roll Two Dice.\n -If the Sum is Even, Player 1 Gets a Point\n -If the Sum is Odd, Player 2 Gets a Point\n";
cout <<endl;
system("pause");
system("cls");
main ();
}



int game()
{ 
    int times;
    int dice1;
    int dice2;
    int sum;
    int even;
    int odd;
    
    system("cls");
    cout << "How Many Times Would You Like to Roll?\n";
    cin >> times;
    while (times>0)
{   
    cout << "Rolls Left :" << times << endl;
    Dice (dice1, dice2, sum);
    times--;}
     if (sum % 2 == 0)
               {even++;
               eventotal=eventotal+1;}
       else 
                {odd++;}
    cout << "Player 1 Points: " << eventotal <<endl;
    cout << "Player 2 Points: " << odd << endl << endl;
}

     


int main()
{
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    int key;
cout << "Menu\n Press 1 for Rules\n Press Any Key to Play\n" ;
cout << "\nWhat is Your Choice: ";
cin >> key;
if (key==1)
   {rules();}
else
   {game();}
  system("pause");
}
I have rewritten your code but I don't know what you are trying to do with the code so you will have to fix it.
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
// dice_game.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;

int even;
int odd;
int eventotal = 0;

void Dice (int& dice1, int& dice2, int& sum)
{	
	dice1 = rand() % (6)+1;
	dice2 = rand() % (6)+1;
	cout << "First Dice    = " << dice1 <<  endl;
	cout << "Second Dice   = " << dice2 << endl;
	sum = dice1 + dice2;
	cout << "The Sum is: " << sum << endl<< endl;
       
//return (dice1, dice2, sum);
}




void rules()
{	
	system("cls");
	cout << " \nThe Rules Are Quite Simple. We Roll Two Dice.\n    -If the Sum is Even, Player 1 Gets a Point\n    -If the Sum is Odd, Player 2 Gets a Point\n";
	cout << endl;
	system("pause");
	system("cls");	
}



void game()
{ 
	int times;
	int dice1;
	int dice2;
	int sum;
	int even;
	int odd;
	int eventotal=0;
	system("cls");
	cout << "\n  How Many Times Would You Like to Roll?\n";
	cout << "      Rolls: ";
	cin >> times;
	system("cls");
	while (times>0)
	{   
		cout << "Rolls Left :" << times << endl;
		if (sum % 2 == 0)
		{
			even++;          
			if (even==1)
			{
				even++;
				eventotal=even;
			}
		}            
		else 
		{
			odd++;
		}
                
		cout << "Player 1 Points: " << eventotal <<endl;
		cout << "Player 2 Points: " << odd << endl << endl;
		Dice (dice1, dice2, sum);
		times--;
	}        
}

     


int main()
{
	system ("color 97");

	srand(unsigned(time(NULL)));
	int key;

	cout << "** Are You Feeling Lucky??? **\n\n";
	cout << "Menu\n   Press 1 for Rules\n   Press Any Key to Play\n" ;
	cout << "\nWhat is Your Choice: ";
	cin >> key;
	if (key==1)
	{
		rules();
	}
	else
	{
		game();
	}
	
	system("pause");
	return 0;
}
Last edited on
Great, i fixed it and now it works. Thanks, however why do i not need to have return for line 24? and Why did u change the random lines on 86? Thanks, i really appericate your help
Line 24: The function return value is void. Nothing needs to be returned. You can write plain old return; but it is not needed.

Line 85: That is how I set a new random seed. You can do it how ever you want though.
Topic archived. No new replies allowed.