C++ Pointer and output not correct

My wins and losses are not reporting correctly. I am not sure if i am using the pointer correctly either. Please help.

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

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

int playGame()
{
int count =0; int random = (rand()%10)+1; int choice;

for(int i=0;i<3;i++)
{
cout << "Guess the number 1-10: ";
cin >> choice;
	if (random==choice)
	{
	cout << "you guessed it right!" <<endl;
	count++; break;
	}
	else if (i==2)
	{
	cout << "you lose!" <<endl;
	break;
	}
	else  
	{
	cout << "Guess again!"<<endl;
	}
}
return count;
}

void wantToPlay(int *win, int *loss)
{
bool perfPlay = true; char option;

while (perfPlay)
	{
		cout << "Would you play to (P)lay or (Q)uit?";
		cin >> option;	
		if (option == 'p' || option == 'P')
		{
			int count=playGame();
			if(count > 0)
			(*win)++;
			else
			(*loss)++;
		}
		else
		perfPlay =false;
	}    
}

int main()
{ 
	int win=0; 
	int loss=0;
	wantToPlay(&win, &loss);
	cout << "You have won " <<win << " games and lost " <<loss <<" games"<<endl;
	return 0;
	system("pause");
}
Last edited on
(*win)++;
You have used the wrong operator to dereference your win and loss variables in line 58. You are getting the memory location instead. Use the * operator, i.e. *win and *loss
THANKS Peter!!!

Slice - does "int win=0;" mean wantToPlay(&win = 0, x )?
I am not sure if every variable in the main needs &.
Last edited on
woops, led you stray there. The variables win and loss are actually ints, not pointers! You don't need the *. Just remove the & symbol and it should work fine.
I edit it and it works now. I am still confused on when to use &. Do I only use & inside a function?
Last edited on
Er... No
The & operator can be used almost anywhere in a program. In your program you need to pass a pointer to the variables win and loss to the function wantToPlay. Using the & operator on an int variable gives you a pointer to an int (int*) which points to that variable.
I would suggest doing some more research on pointers if you want to program in c++. Google is your friend.
In wantToPlay() all variable requires *win or *loss.
In int main() only some variable require &win or &loss.

So why int win=0 and not int &win=0
why cout << "You have won " <<win <<
and not cout << "You have won " <<&win <<
I hope you won't be offended if I say that it sounds like you don't really understand how pointers work. The answers to your questions require an understanding of the difference between a variable and a pointer to a variable. That requires some independent study, and is not something that can really be answered in a forum post. However there are millions of tutorials on the web that can teach you about such things, I suggest you read one or two of them.
This is my first time doing C++, my logic and vocab is sub-par. I was hoping there was a rule of thumb in case I need to use it in the future instead of doing "guess and check". I am happy with the help I have received, so no worries.
Topic archived. No new replies allowed.