Returning a value.

Hi,

Can someone have a look at this code please. I am trying to get a function to return a value but it doesn't seem to work. I know i am doing something wrong but I can't seem to see what it is.

I'm new to programming please go easy.

I want to return the value for the variable called valid.

If someone can help me I appreciate you time and 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
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
119
120
121
bool validate(int row, char col, char flag, char block, int size, int count, char move, bool valid)
{
	char response;
	char QMark = 63;


	
	if (row < 0 || col < 0 || row >= size || col >= size)
		{
			cout<<" You have entered invalid coordinates! "<<endl<<endl;
			return valid=0;// I want the program to return these values if the conditions are met
		}
	else if(playerMove[row][col] != block &&  playerMove[row][col] != flag && playerMove[row][col] != QMark && move == 'm' || move == 'M')
		{		
			cout<<"You have already used these coordinates!"<<endl<<endl;
			return valid=0;
		}
	else if(playerMove[row][col] != block && playerMove[row][col] != flag && move == 'f' || move == 'F')
	{
		cout<<"You have already used these coordinates!"<<endl<<endl;
		return valid = 0;
	}
	   else if(move == 'm' && playerMove[row][col] == flag)																			
			{
				cout<<"There is a flag on these coordinates, would you like to remove it? (Y \\ N): ";
				cin>>response;
				cout<<endl;
				if(response == 'y')
		     	{
			       playerMove[row][col] = QMark;
				  
			    }

		        return valid=0;
	         }
	   else
		 return  valid=1;
}

void userInput(int numberMines, int size, int sizeTwo)
{
	char move,  block = 178, flag = 70, bomb = 245;
	int row, col, count = 0, flagcount = 0;
	bool win = false;
	bool valid = 1;//I think its something to do with this variable. I have to intitalise it for the program to run.


	do
	{	
			
			displaygrid(size);
			cout<<"Please enter column number: ";
			while(!(cin >> col))
			{
				cout<<endl;
				cout<<"You must enter a number!"<<endl;
				cin.clear();
				cin.ignore(1);
				cout<<endl;

				cout<<"Please enter column number: ";
			}

			cout<<"please input row number: ";
			while(!(cin >> row))
			{
				cout<<endl;
				cout<<"You must enter a number!"<<endl;
				cin.clear();
				cin.ignore(1);
				cout<<endl;

				cout<<"please input row number: ";
			}

			cout<<endl;
			cout<<"Please enter F to indicate a bomb position or M for a move: ";
			cin>>move;
			cout<<endl<<endl;

			validate(row, col, flag, block, size, count, move, valid);//This function is called and the i want it to check the players move then return the value of the valid variable. 
			if (valid==1)
			{
				count++;						
				if(move == 'f' || move == 'F')
				{
					playerMove[row][col] = flag; 
					count--;
					if(valid == 0)
					{
						count--; 
								 
					}
					if(mines[row][col] == bomb)
					{
						flagcount++;
					}
				}
				else playerMove[row][col] = mines[row][col];
				
			}
				if(count == sizeTwo && flagcount == numberMines)
				{
					win = true;
				}
			
	}
	while(win != true && playerMove[row][col] != bomb);
			
			
			if(win == true)
			{
				displayLoss(size);
				cout<<" YOU WIN! "<<endl<<endl;
			}
				else if(playerMove[row][col] == bomb)
				{
					displayLoss(size);
					cout<<" GAME OVER! "<<endl<<endl;
				}
}
Last edited on
Your validate() function is returning a value, but your calling code is not reading the returned value.

Your calling code seems to be expecting the value of its own valid to be changed by the function. However, that's not going to change, because you're passing that value by copy, not by reference.

You need to either:

- have your calling code read the value that the function is returning, or
- modify your function arguments so that valid is passed by reference.
Thanks MikeyBoy. I'll have a look into passing by reference now and try that.
Last edited on
MikeyBoy you're an absolute legend. Just watched a couple of videos on that referencing and it works.
Last edited on
No problem - glad it worked out!

It's also worth reminding yourself how you read the value that's returned from a function. Even if you decide to use pass-by-reference for the functions you write, you'll need to use standard library functions that return values at some point.
Cheers MikeyBoy. In fact while we're on the subject of passing values, can you tell me how to use two dimensional arrays with procedures. I have had look online but I can't seem to find anything that goes into great detail.

Thanks again.
Last edited on
Topic archived. No new replies allowed.