Setter function not working?

Hello I'm trying to get a setter function to store an integer, but the data entered stored end up weird when I try to access them (610861952 & 32764). I'm not very familiar with splitting up files so I'm sure it's a simple mistake... but I can't tell what's wrong.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Ant

{
	private:

	//keeps track of where the ant is
	int xRow;
	int yCol;

	//tracks ant orientation
	int compass;

	public:

	void setRow(int rowIn);
	void setCol(int columnIn);
};

#endif


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
/*****************
 * setRow
 * Takes integer as parameter and sets private variable xRow
 * **************/
void Ant::setRow(int rowIn)
{
	xRow=rowIn;
}

/*******************
 * setCol
 * Takes integer as parameter and sets private variable yCol
 ******************/

void Ant::setCol(int columnIn)
{
	yCol=columnIn;
}
/*****************
 * moveAnt
 * Will return orientation of the ant
 * **************/
void Ant::moveAnt(int a, int boardRow, int boardCol, char** board)
{

//when I try to access the variables in this other function that's when the weird numbers come up. 

	std::cout << "xRow " << xRow << std::endl;
	std::cout << "yCol " << yCol << std::endl;
.
.
.


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
/***************
not actually a main function , but I made it one for testing purposes. It's a menu function that passes variables to a main function.
***************/

int main ()
{
Ant bug; //declare ant object
        //get ant start row
	std::cout << "What row will your ant start in? (Please choose a number 1-5)" << std::endl;
	
		
	while (!(startRow>0 && startRow<=5)) //while ant row is not between 1 and upper row limit
	{
		std::cout << "Value is off the board, please try again." << std::endl;
		startRow = getInt();
		startRow--; //need to subtract one or else display will be off
	}
			
	bug.setRow(startRow);
	std::cout << "The ant will start in row " << startRow+1 << "." << std::endl;


	std::cout << "What column will your ant start in? (Please choose a number 1-5)" << std::endl;

	startCol = getInt();
	while (!(startCol>0 && startCol<=5)) //while ant col not between 1 and lower limit
	{
		std::cout << "Value is off the board, please try again." << std::endl;
		startCol = getInt();
		startCol--; //need to subtract by one or else display will be off
	}
			
	bug.setCol(startCol);
	std::cout << "The ant will start in column " << startCol+1 << "." << std::endl;

return 0;
}


This is the input validation function I made to use for this project
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
int getInt()
{
	int num;

	while (true) //perform loop until conditions are met
	{
		std::cin >> num;
	
		while (std::cin.fail()) //if extraction fails
		{
			std::cin.clear(); //reset operation to normal
			std::cin.ignore(1000, '\n'); //ignore bad input
			std::cout << "Oops! That's not an integer. Please try again" << std::endl; //let user know error
			std::cin >> num;//ask for input again
		
			if(!(std::cin.fail()))
			{
				break;
			}
		}

	
		std::cin.ignore(1000, '\n'); //ignore bad input
		while (std::cin.gcount() > 1) //if ignored input more than 1 spot
		{
			std::cout << "Oops! That's not an integer. Please try again" << std::endl; //let user know
			std::cin >> num; //ask for input again
			
			std::cin.ignore(1000, '\n');
			if(std::cin.gcount() <= 1)
			{
				break;
			}

		}
	
		break; //exit loop
		
	}

	return num;
}




Last edited on
Alas, I don’t have time (or mental capacity) right now to debug this entirely for you, but the problem is not in your setter. The value is being goobered somewhere else.

Take a close look at what happens any place you modify them.
Your dummy main() is using startRow uninitialised, but who knows what is true in the real one.

You aren't ACCESSING them anywhere in the given code, because you don't call moveAnt() anywhere. It's like all your previous threads - you haven't given adequate information.
Topic archived. No new replies allowed.