My Game Will Not Add Odds

Thanks
Last edited on
Have you tried compiling this? I don't think this would compile. If the function starts with void you do not need to return any value or call main(). The function will automatically return to main and continue the program.

Did you pay any attention to my previous code? Calling the dice function in your code does nothing. You have to pass in the variable sum as a reference like I did.

You don't even need to pass in dice1, and dice2. You can just initialize them in the function.
Last edited on
You are not understanding parameters to functions and return values from functions.

Line 52 calls Dice() but ignores the return value. Dice() returns the sum. Dice does not modify game()'s sum variable; sum remains uninitialized in game().
Dice() also takes two parameters (dice1 and dice2) for no reason, as again they are uninitialized in game() and game()'s dice1 and dice2 variables are not modified by Dice().



Try this :wink:
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
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;


void Dice (int& sum)
{
	int dice1, dice2;
	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;
}


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\n";
	
	system("pause");
	system("cls");	
}


void game()
{ 
	int times = 0;
	
	
	int sum;
	int even = 0;
	int odd = 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;
		
		Dice(sum);

		if (sum % 2 == 0)
		{
			even++;			
		}            
		else 
		{
			odd++;
		}
                
		cout << "Player 1 Points: " << even <<endl;
		cout << "Player 2 Points: " << odd << endl << endl;
		
		--times;
	}

	if(even > odd)
	{
		cout << "Player 1 wins!\n";
	}
	else if(even < odd)
	{
		cout << "Player 2 wins!\n";
	}
	else
	{
		cout << "It's a tie\n";
	}
	
}

     


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

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

	cout << "** Are You Feeling Lucky??? **\n\n";
	cout << "Menu\n   Press 1 for Rules\n   Press any other #'s to Play\n" ;
	cout << "\nWhat is Your Choice: ";
	cin >> key;
	if (key==1)
	{
		rules();
	}
	else
	{
		game();
	}
	
	system("pause");
	return 0;
}
If i don't have the main like you said, once it runs the rules, it says "press any key to continue" then just exits our of the program. Same with the game, after its done, it just ends instead of returning to the home screen.

What are parameters?
whats the difference if i say

dice (int dice1, int dice2);

or

dice();
int dice 1;
int dice 2;

i am new at comp prog so i have no idea how any of this works

thanks
You will have to add a while loop to run the program again then.
Topic archived. No new replies allowed.