Trying to get a function to return an input

On line 35 I'm trying to return the int incrd which the user has inputted but i get a window which pops up saying:
Run Time Check failure #3 - the variable "incrd" is being used without being initialized.
I think its because when I initialize incrd its outside the scope of GetMove() but i want to be able to use the function to get the users input so I'm not sure what to do. Any help would be appreciated, 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 things
#include <ctime>
using namespace std;

void PrintBoard(int b[8][8])          // define the function to print the board
{
	cout << "  1 2 3 4 5 6 7 8" << endl;
	for(int i=0;i<8;i++)  // loop 8 times for 8 lines
	{
		cout << " -----------------" << endl;
		cout << (i + 1) <<"|";
		for(int o=0;o<8;o++)  // loop for the 8 elements on the line
		{
			if (b[i][o] == 1)
			{cout << " ";}  // display a blank space
			if (b[i][o] == 0)
			{cout << " ";}  // display a blank space
			cout << "|";
		}
	cout << endl;            // go to a new line to display the next line of matrix
	}
	cout << " -----------------" << endl;
}

int GetMove(int incrd)
{
	cout << "Type in the co-ordinates of the square " << endl << "you would like to click in the form xy"<< endl<< endl;                                      // gets the players move and makes sure its on the board
	while(true)
	{
		incrd = 1;
		cin >> incrd;
		if ((10<incrd && incrd < 19)  || (20<incrd && incrd<29) || (20<incrd && incrd<29) || (30<incrd && incrd<39) || (40<incrd && incrd<49) || (50<incrd && incrd<59) || (60<incrd && incrd<69) || (70<incrd && incrd<79) || (70<incrd && incrd<79))
		{
			return incrd;
			break;
			
		}
		cout <<"invalid input, please enter co-ordinates in the correct form (xy)" << endl;
	}
}





int main()											// MINESWEEPER text based - input a co-ord in order to click a square just like normal minus the mouse
{											        // eg         "input a co ordinate"
													//                      3 4      == [3][4]
													//            -----------
													//            |   1X1   |          <--- somewhat like that where X = bomb 
													//            |   111   |



	int b[8][8];    //declare matrix for board
	srand ( (unsigned int)time(NULL) );		
	int bombs = 0;                     // control no of bombs
	int temp = 1;						// temp number for making board
	while ( bombs != 10 )           // sets up the minimum number of mobs on the board at once
	{
		int cx = 0;						// x and y co-ords for the bombs location (b array)
		int cy = 0;
		bombs = 0;						// setting bombs to zero

		for(int ctl = 0; ctl < 8; ctl += 1)           //defining the minesweeper board [8*8]
		{
			cx = 0;
			for(int cnt = 0; cnt < 8; cnt += 1)
			{
				temp = rand() % 6;              // temp = random number between 0 and 5
				if ( temp < 1 )                // if temp == 0
				{b[cx][cy] = 1;}			// then theres a bomb on [cx]cy]
				if ( temp > 0 )
				{b[cx][cy] = 0;}			//if temp != 0 then no bomb on [cx][cy]		
				if (b[cx][cy] == 1)						// if theres a bomb on [cx][cy]
				{bombs += 1;}						// add one onto the counter
				cx += 1;							// repeat for the rest of the line
			}
			cy += 1;								// repeat for the next line and so on

		}										//	breaks out of loop if theres enough bombs
	}

	cout << "---Welcome To Minesweeper--- " << endl;
	cout << "Creating Minefield... Created" << endl << endl;
	PrintBoard(b);
	int incrd;
	GetMove(incrd);
	cout << "Move: " << incrd;
		

}
So one thought is this:
Do not pass an argument to your GetMove function at all.Instead do something like this :

int incrd = GetMove();

Then your function should be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int GetMove()
{
	cout << "Type in the co-ordinates of the square " << endl << 
			"you would like to click in the form xy" << endl << endl;
	while (true)
	{
		int incrd;
		cin >> incrd;
		if ((10 < incrd && incrd < 19) || (20 < incrd && incrd < 29) || 
			(20 < incrd && incrd < 29) || (30 < incrd && incrd < 39) || 
			(40 < incrd && incrd < 49) || (50 < incrd && incrd < 59) || 
			(60 < incrd && incrd < 69) || (70 < incrd && incrd < 79) || 
			(70 < incrd && incrd < 79))
		{
			return incrd;
		}
		cout << "invalid input, please enter co-ordinates in the correct form (xy)" << endl;
	}
}
Thanks it works perfectly now!
Another take?

Use mod (%) and integer division (/) to chop the input coord up to simplify the tests (easier to read?) And could even return x and y using out params or a std::pair<> return code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int GetMove()
{
    cout << "Type in the co-ordinates of the square " << endl << 
            "you would like to click in the form xy" << endl << endl;
    while (true)
    {
        int incrd = 0;
        is >> incrd;
        int x = incrd % 10;
        int y = incrd / 10;
        if ((1 <= x) && (x <= 8) && (1 <= y) && (y <=8))
        {
            return incrd;
        }
        cout << "invalid input, please enter co-ordinates in the correct form (xy)" << endl;
    }
}


Andy
Topic archived. No new replies allowed.