Writing programs with multiple functions

hi, i recently wrote some code for a school project and before i start this question, i am not asking you to do this for me, i have just encountered an error that i would like your advice on

anyways,
we were instructed to make a program that would simulate the dice game "pig"

what i would like to know is why my code insists on giving the player one turn,
then the computer turn, and then give the player two turns, and give the computer one turn, and then again give the player two turns, and give the computer one turn

i know my question isn't phrased the best i would really appreciate some help!
thank very much =)

here is my code:

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
118
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int humanTurn(int &Humantotalscore);
int computerTurn(int &Computertotalscore);
int roll ();
int turn = 0;
int d = 0;

int main ()
{
	srand(time(NULL));
	int Humantotalscore = 0;
	int Computertotalscore = 0;

	while ((humanTurn(Humantotalscore) < 100) && (computerTurn(Computertotalscore) <100))
	{
	if ((humanTurn(Humantotalscore) >= 100) && (computerTurn(Computertotalscore) <100))
	{
			cout << "congradulations you win!" << endl;
			cout << "       =D\n";
	}

	else if ((Computertotalscore >= 100) && (Humantotalscore < 100))
	{
			cout << "sorry, you have lost" <<endl;
			cout << "       =(\n";
	}
	}
	return 0;
}

int humanTurn(int& Humantotalscore)
{
	int Humanscorethisturn = 0;
	int Scoreperroll = 0;
	char choice = 'r';
	char roll = 'r';

	Humanscorethisturn = 0;

	while ((Scoreperroll != 1) && (choice == 'r') && (Humantotalscore <100))
	{
		Scoreperroll = (rand() %6 +1);

		if(Humanscorethisturn == 0)
		{
		cout << "please press r to roll: ";
		cin >> roll;
		}

		if (Scoreperroll >= 2)
		{
			if (Humanscorethisturn ==0)
			{
			Humanscorethisturn = 0;
			}

			else if (Humanscorethisturn != 0)
			{
				Humanscorethisturn = Humanscorethisturn;
			}

			Humanscorethisturn =  Scoreperroll + Humanscorethisturn;
			cout << "You have rolled a " << Scoreperroll <<"\n" << "Your score this turn is: " << Humanscorethisturn << "\n" << " Please press r to roll again or h to hold: ";
			cin >> choice;
		}
		
		else
		{
			Humanscorethisturn = 0;
			cout << "You have rolled a 1, End of turn\n" ;
		}		
	}
	Humantotalscore = Humantotalscore + Humanscorethisturn;
	cout << "Your totall score is: " << Humantotalscore << "\n";
	return (Humantotalscore);
}

int computerTurn(int& Computertotalscore)
{
	int Computersscorethisturn = 0;
	int Scoreperroll = 0;
	char choice = 'r';

	Computersscorethisturn = 0;

	while ((Computertotalscore < 100) && (choice == 'r') && (Scoreperroll != 1) && (Computersscorethisturn < 20))
	{

		Scoreperroll = (rand() %6 +1);
		if (Scoreperroll >= 2)
		{
			if (Computersscorethisturn == 0)
			{
				Computersscorethisturn = 0;
			}

			else if (Computersscorethisturn != 0)
			{
				Computersscorethisturn = Computersscorethisturn;
			}
			Computersscorethisturn = Computersscorethisturn + Scoreperroll;
			cout << "the computer has rolled a: " << Scoreperroll << "\n" << "the computer's score this turn is: " << Computersscorethisturn << "\n";
		}

		else if (Scoreperroll == 1)
		{
			Computersscorethisturn = 0;
			cout << "The computer has rolled a 1, End of turn\n";
		}
	}
	Computertotalscore = Computertotalscore + Computersscorethisturn;
	cout << "The computer's total score is: " << Computertotalscore <<"\n";
	return (Computertotalscore);
}

Last edited on
1
2
3
while ((humanTurn(Humantotalscore) < 100) && (computerTurn(Computertotalscore) <100))
{
if ((humanTurn(Humantotalscore) >= 100) && (computerTurn(Computertotalscore) <100))


When you use the && operator, the compiler won't execute the 2nd half of the statement if the first half is false... because it knows the entire expression will be false. It's often referred to as "short circuiting"

To put it in other terms...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this...
if(foo && bar)
{
  x();
}

// is the same as this:
if(foo)
{
  if(bar)
  {
    x();
  }
}


Notice how in the 2nd example, bar won't happen if foo is false. If bar is a function call, that means the function will never be called.

The solution is to not short circuit. Do this by moving your function calls outside of the if statement:

1
2
3
4
5
6
while ((Humantotalscore < 100) && (Computertotalscore < 100))
{
  humanTurn(Humantotalscore);
  computerTurn(Comptertotalscore);

  if ((Humantotalscore >= 100) && (Computertotalscore <100))




On a side note: if you are passing the out of the function by reference, it doesn't make sense to return it. Likewise if you are returning it, it doesn't make sense to pass by reference. Pick one and go with it. Personally, I recommend getting rid of the pass-by-reference and just return the value:

1
2
3
4
5
6
7
8
int humanTurn(int Humantotalscore)  // pass by value
{
  // ... blah blah ...
}

// when you call it:

Humantotalscore = humanTurn(Humantotalscore);
Last edited on
i understand what you're saying but i don't see how short circuiting would affect the
1
2
int humanTurn(int &Humantotalscore);
int computerTurn(int &Computertotalscore);


that's what giving me problems is,
even though
1
2
int humanTurn(int &Humantotalscore);
int computerTurn(int &Computertotalscore);


is in chronological error,
int humanTurn(int &Humantotalscore);
will initiate twice per cycle

whereas
int computerTurn(int &Computertotalscore);
only initiates once per cycle
The problem is not with the functions. It's with how you're calling the functions. Take a look:

if ((humanTurn(Humantotalscore) >= 100) && (computerTurn(Computertotalscore) <100))

Here, your intent is to call humanTurn and computerTurn so that each of them get a turn, right?

The problem is, only humanTurn is being called, because (humanTurn(Humantotalscore) >= 100) is coming back as false. Therefore the 2nd half of the expression:

(computerTurn(Computertotalscore) <100))

is being skipped, resulting in the computer's turn being skipped.


You're doing the same thing in your while loop condition (and that shouldn't even be calling the functions anyway).
ah, i finally worked it out thanks to your first piece of advice, thank you very much for your help, sorry i didn't understand the first time and had to ask you to explain again but i appreciate the fact that you did

tyvm!!!!! =)
Topic archived. No new replies allowed.