Logical error

Hi! Sorry im posting about this more! Iv'e worked on this some more and now when I debug it, it keeps repeating the "while" loop in the beginning:

1
2
3
4
5
6
7
while (grid[6][9] != player)
	{
		cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;
			void showgrid();
	}

The strange thing is that it doesn't re-show the grid that I added at the end of the loop: void showgrid();.


After I type in "down", for example, it doesn't check if the move is valid, or move the player position. It just repeats
1
2
3
cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;

It seems like the program is skipping over the rest of the code. It was working earlier! I'd appreciate anyone's comments
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Dungeon Crawler.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string imput;
	char player = 'G', trap = 'T', goal = 'X';
	char grid[7][10];
	int row, col, x = 0, y = 0;
	void showgrid();
	
	for(row = 0; row < 7; row++)// creates the grid 
	{
		for (col = 0; col < 10; col++)
		grid[row][col] = '.';
		
	}

	grid[x][y] = player; //Gives player's beginning position 
	grid[2][3] = trap; //Gives trap positions
	grid[3][6] = trap;
	grid[4][9] = trap;
	grid[5][4] = trap;
	grid[6][8] = trap;
	grid[6][9] = goal; //Gives finishing position
 
	void showgrid();
	{
		for (row = 0; row < 7; row++)  // Shows grid
		{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
		}
	}

	while (grid[6][9] != player)
	{
		cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;
			void showgrid();
	}

		if(imput != "up", "down", "left", "right", "quit")
		{
			cout << "That is not a valid imput. Please imput one of the following\n" 
				"up, down, left, right, or quit";
		}
			
		if(imput == "up")
		{
				if( y == 0 && x > -1 && x < 10)
				{
					cout << "You cannot move up from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y--] = player; //re-locates player's position if move is valid
				}
		}

		
		if(imput == "down")
		{
				if(y == 6 && x > -1 && x < 10)
				{
					cout << "You cannot move down from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y++] = player; //re-locates player's position if move is valid

				}
		}

		if(imput == "left")
		{
				if( y > -1 && y < 6 && x == 0)
				{
					cout << "You cannot move left from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x--][y] = player; //re-locates player's position if move is valid
				}
		}

		if(imput == "right")
		{
				if( y > -1 && y < 10 && x == 9)
				{
					cout << "You cannot move right from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x++][y] = player; //re-locates player's position if move is valid
				}
		}
		
		if(imput == "quit") //quits game if player chooses this command
		{
			return(0);
		}
		
		//the following is what happens if the player lands on one of the traps
		if(grid[2][3] == player || grid[3][6] == player || grid[4][9] == player || grid[5][4] == player || grid[6][8] == player) 
		{
			cout << "it's a trap!\n";
			return(0);
		}

		if(grid[6][9] == player) //What happens if player successfully makes it to the goal
		{
		cout << "You successfully escaped the dungeon!\n";
		}

		return(0);
	}

	
 
    void showgrid();
is a declaration of the function prototype. This should go somewhere before the start of main()

If you actually want to call the function, do it like this:
 
    showgrid();


To define the function (outside of main() ) do something like this:
1
2
3
4
void showgrid()
{
    // code for the function goes here
}


See Declaring functions here for an example: http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
When I define the function outside of the main function, it says the variables are undefined because they are all local variables. What do I do if they are local and the void is outside the main? Make then global variables?
Making variables global can sometimes be done. But usually a better solution is to pass as parameters whichever variables the function requires.
Your original code is invalid. You think that below there is a function definition

1
2
3
4
5
6
7
8
9
10
	void showgrid();
	{
		for (row = 0; row < 7; row++)  // Shows grid
		{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
		}
	}


But you may not define a function inside another function. Why does not the compiler issue an error? Because it is not a function definition. At the end of the first line there is a semicolon:

void showgrid();

So the compiler considers the code as it consists from two parts: 1) function declaration

void showgrid();

and below the declaration some code block

1
2
3
4
5
6
7
8
9
	{
		for (row = 0; row < 7; row++)  // Shows grid
		{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
		}
	}


To correct the code you should 1) remove the semicolon 2) declare a function parameter.

So the valid code is

1
2
3
4
5
6
7
8
9
10
	void showgrid( char grid[][10] )
	{
		for (row = 0; row < 7; row++)  // Shows grid
		{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
		}
	}


This function definition you should place outside main.
In main or before main you should place its declaration

void showgrid( char grid[][10] );

Maybe there are other errors but I did not look through all your code because it is already enough that your code is invalid.





Last edited on
Ok, can you check if I passed the variables as parameters? I put the "void showgrid (parameters)" outside the main function and called on it as showgrid (parameters); inside of my functions. The variables are col, row, and grid[7][10]. Do I need to include the variables within the parentheses everytime I call on the function? Also, if I declared the variables, (int row, int col, char grid[7][10]), outside the main function, do I still have to declare them as local variables?
Thanks for all your help Chervil! cheers

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Dungeon Crawler.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


void showgrid(int row, int col, char grid[7][10])
{
	for (row = 0; row < 7; row++)  // Shows grid
	{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	string imput;
	char player = 'G', trap = 'T', goal = 'X';
	char grid[7][10];
	int x = 0, y = 0, row, col;
	

	for(row = 0; row < 7; row++)// creates the grid 
	{
		for (col = 0; col < 10; col++)
		grid[row][col] = '.';
		
	}

	grid[x][y] = player; //Gives player's beginning position 
	grid[2][3] = trap; //Gives trap positions
	grid[3][6] = trap;
	grid[4][9] = trap;
	grid[5][4] = trap;
	grid[6][8] = trap;
	grid[6][9] = goal; //Gives finishing position
 
	

	while (grid[6][9] != player)
	{
		cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;
			showgrid(int row, int col, char grid[7][10]);
	}

		if(imput != "up", "down", "left", "right", "quit")
		{
			cout << "That is not a valid imput. Please imput one of the following\n" 
				"up, down, left, right, or quit";
		}
			
		if(imput == "up")
		{
				if( y == 0 && x > -1 && x < 10)
				{
					cout << "You cannot move up from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y--] = player; //re-locates player's position if move is valid
				}
		}

		
		if(imput == "down")
		{
				if(y == 6 && x > -1 && x < 10)
				{
					cout << "You cannot move down from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y++] = player; //re-locates player's position if move is valid

				}
		}

		if(imput == "left")
		{
				if( y > -1 && y < 6 && x == 0)
				{
					cout << "You cannot move left from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x--][y] = player; //re-locates player's position if move is valid
				}
		}

		if(imput == "right")
		{
				if( y > -1 && y < 10 && x == 9)
				{
					cout << "You cannot move right from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x++][y] = player; //re-locates player's position if move is valid
				}
		}
		
		if(imput == "quit") //quits game if player chooses this command
		{
			return(0);
		}
		
		//the following is what happens if the player lands on one of the traps
		if(grid[2][3] == player || grid[3][6] == player || grid[4][9] == player || grid[5][4] == player || grid[6][8] == player) 
		{
			cout << "it's a trap!\n";
			return(0);
		}

		if(grid[6][9] == player) //What happens if player successfully makes it to the goal
		{
		cout << "You successfully escaped the dungeon!\n";
		}

		return(0);
	}

	
The compiler shall issue an error. It is an invalid function call

showgrid(int row, int col, char grid[7][10]);

Instead of using arguments you declared parameters.
Also the function definition is invalid. Instead of

1
2
3
4
5
6
7
8
9
10
void showgrid(int row, int col, char grid[7][10])
{
	for (row = 0; row < 7; row++)  // Shows grid
	{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
	}
}


should be

1
2
3
4
5
6
7
8
void showgrid(char grid[][10], int rows )
{
	for ( int i = 0; i < rows; i++ )	
	{
		for ( int j = 0; j < 10; j++ )  cout << grid[i][j] << ' ';
		cout << "\n";
	}
}
Last edited on
Ok thanks for all your imput Vlad! Your imput is very appreciated! Since i am new to c++, I am a little confused by your function definition.

First, you said, "Instead of using arguments you declared parameters." What do you mean by this? I thought I want to declare parameters in my def..

Second, why did you take out the 7 in char grid[][10]. Since the array is supposed to be a two demensional array of 7 by 10, I thought it's written as grid[7][10].

Lastly, I still receive an error that the "function does not take 0 arguments" I made your corrections.

1
2
3
4
5
6
7
while (grid[6][9] != player)
	{
		cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;
			showgrid(char grid][][10], int rows);
	}


This part char grid appears red and it says "type name is not allowed". Do you know the problem to this?
One again it is not a function call

showgrid(char grid][][10], int rows);

Instead of passing arguments you declared parameters.

As for the declaration I showed then arrays passed by value implicitly are converted to a pointer to their first element. So all listed function declarations are equivalent and declare the same function

void showgrid(char ( *grid )[10], int rows );
void showgrid(char grid[][10], int rows );
void showgrid(char grid[7][10], int rows );
void showgrid(char grid[50000][10], int rows )
Last edited on
Topic archived. No new replies allowed.