Ant simulator

How do I solve my problem where my if statement doesn't follow parameters

#include<iostream>
#include<string>
#include<vector>

using namespace std;
int main(){
int y,z,rows,columns;
string s;
cin>>rows>>columns;
cin>>y>>z>>s;
int matrix[rows][columns];
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
cin>>matrix[i][j];
}
}
if(s == "N"){
if(matrix[y][z] = 1){
matrix[y][z] = 0;
if(1 <= z < (columns+1)){
cout<<y<<" "<<z-1<<" "<<"W"<<endl;
}
else{
cout<<y<<" "<<columns-1<<"W"<<endl;
}
}
else{
matrix[y][z] = 1;
if(-1 <= z < (columns+1)){
cout<<y<<" "<<z+1<<" "<<"E"<<endl;
}
else{
cout<<y<<" "<<0<<" "<<"E"<<endl;
}
}
}
else if(s == "E"){
if(matrix[y][z] = 1){
matrix[y][z] = 0;
if(1 <= y < (rows+1)){
cout<<(y-1)<<" "<<z<<" "<<"N"<<endl;
}
else{
cout<<(rows-1)<<" "<<z<<" "<<"N"<<endl;
}
}
else{
matrix[y][z] = 1;
if(-1 <= y < (rows-1)){
cout<<y+1<<" "<<z<<" "<<"S"<<endl;
}
else{
cout<<0<<" "<<z<<" "<<"S"<<endl;
}
}
}
else if(s == "S"){
if(matrix[y][z] = 1){
matrix[y][z] = 0;
if(-1 <= z < (columns-1)){
cout<<y<<" "<<(z+1)<<" "<<"E"<<endl;
}
else{
cout<<y<<" "<<0<<" "<<"E"<<endl;
}
}
else{
matrix[y][z] = 1;
if(1 <= z < (columns+1)){
cout<<y<<" "<<z-1<<" "<<"W"<<endl;
}
else{
cout<<y<<" "<<columns-1<<" "<<"W"<<endl;
}
}
}
else{
if(matrix[y][z] = 1){
matrix[y][z] = 0;
if(-1 <= y < rows){
cout<<y+1<<" "<<z<<" "<<"S"<<endl;
}
else{
cout<<0<<" "<<z<<" "<<"S"<<endl;
}

}
else{
matrix[y][z] = 1;
if(1 <= y < (rows+1)){
cout<<y-1<<" "<<z<<" "<<"N"<<endl;
}
else{
cout<<(rows-1)<<" "<<z<<" "<<"N"<<endl;
}
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
cout<<matrix[i][j];
}
cout<<endl;
}
return 0;
}
@UnkownUser

On your 17th line, if(matrix[y][z] = 1){, you're using a single equal symbol, which assigns a 1, instead of checking the contents with a double equals. Change that and you may get better results
Well first it would help if you used code tags to format the code so that it is readable!


[code]
code goes here
[/code]



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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
	int y, z, rows, columns;
	string s;

	cin >> rows >> columns;
	cin >> y >> z >> s;

	int matrix[rows][columns];

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < columns; j++) {
			cin >> matrix[i][j];
		}
	}

	if (s == "N") {
		if (matrix[y][z] = 1) {
			matrix[y][z] = 0;
			if (1 <= z < (columns + 1)) {
				cout << y << " " << z - 1 << " " << "W" << endl;
			}
			else {
				cout << y << " " << columns - 1 << "W" << endl;
			}
		}
		else {
			matrix[y][z] = 1;
			if (-1 <= z < (columns + 1)) {
				cout << y << " " << z + 1 << " " << "E" << endl;
			}
			else {
				cout << y << " " << 0 << " " << "E" << endl;
			}
		}
	}
	else if (s == "E") {
		if (matrix[y][z] = 1) {
			matrix[y][z] = 0;
			if (1 <= y < (rows + 1)) {
				cout << (y - 1) << " " << z << " " << "N" << endl;
			}
			else {
				cout << (rows - 1) << " " << z << " " << "N" << endl;
			}
		}
		else {
			matrix[y][z] = 1;
			if (-1 <= y < (rows - 1)) {
				cout << y + 1 << " " << z << " " << "S" << endl;
			}
			else {
				cout << 0 << " " << z << " " << "S" << endl;
			}
		}
	}
	else if (s == "S") {
		if (matrix[y][z] = 1) {
			matrix[y][z] = 0;
			if (-1 <= z < (columns - 1)) {
				cout << y << " " << (z + 1) << " " << "E" << endl;
			}
			else {
				cout << y << " " << 0 << " " << "E" << endl;
			}
		}
		else {
			matrix[y][z] = 1;
			if (1 <= z < (columns + 1)) {
				cout << y << " " << z - 1 << " " << "W" << endl;
			}
			else {
				cout << y << " " << columns - 1 << " " << "W" << endl;
			}
		}
	}
	else {
		if (matrix[y][z] = 1) {
			matrix[y][z] = 0;
			if (-1 <= y < rows) {
				cout << y + 1 << " " << z << " " << "S" << endl;
			}
			else {
				cout << 0 << " " << z << " " << "S" << endl;
			}

		}
		else {
			matrix[y][z] = 1;
			if (1 <= y < (rows + 1)) {
				cout << y - 1 << " " << z << " " << "N" << endl;
			}
			else {
				cout << (rows - 1) << " " << z << " " << "N" << endl;
			}
		}
	}

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < columns; j++) {
			cout << matrix[i][j];
		}
		cout << endl;
	}
	return 0;
}


Also, this isn't standard C++ code. See L14 above. In standard C++ the size of an array has to be known at compile time. The number of elements can't be set at run-time as here. If you need a run-time sized container, then the best to use is std::vector. Replace L14 with

 
std::vector<std::vector<int>> matrix(rows, std::vector<int>(columns));


Also L25 (and others) doesn't do what you might think. In C++ it doesn't work like it does in maths. Each part of the conditional has to be expressed separately. so L25 would be:

 
			if (l <= z && z < columns + 1) {



L23 and others. = is the assignment operator. == is the equality conditional test.
And
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

@whitenite1, it ain't an issue on just line 17, there are 4 instances of using = instead of ==. Using code tags would be easier to pinpoint what lines are borked.
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
#include<iostream>
#include<string>
#include<vector>

using namespace std;
int main()
{
   int y, z, rows, columns;
   string s;
   cin >> rows >> columns;
   cin >> y >> z >> s;
   int matrix[rows][columns];  // run-time sized regular arrays not allowed in C++
   for (int i = 0; i < rows; i++)
   {
      for (int j = 0; j < columns; j++)
      {
         cin >> matrix[i][j];
      }
   }
   if (s == "N")
   {
      if (matrix[y][z] = 1)  // oops!
      {
         matrix[y][z] = 0;
         if (1 <= z < (columns + 1))
         {
            cout << y << " " << z - 1 << " " << "W" << endl;
         }
         else
         {
            cout << y << " " << columns - 1 << "W" << endl;
         }
      }
      else
      {
         matrix[y][z] = 1;
         if (-1 <= z < (columns + 1))
         {
            cout << y << " " << z + 1 << " " << "E" << endl;
         }
         else
         {
            cout << y << " " << 0 << " " << "E" << endl;
         }
      }
   }
   else if (s == "E")
   {
      if (matrix[y][z] = 1)  // oops!
      {
         matrix[y][z] = 0;
         if (1 <= y < (rows + 1))
         {
            cout << (y - 1) << " " << z << " " << "N" << endl;
         }
         else
         {
            cout << (rows - 1) << " " << z << " " << "N" << endl;
         }
      }
      else
      {
         matrix[y][z] = 1;
         if (-1 <= y < (rows - 1))
         {
            cout << y + 1 << " " << z << " " << "S" << endl;
         }
         else
         {
            cout << 0 << " " << z << " " << "S" << endl;
         }
      }
   }
   else if (s == "S")
   {
      if (matrix[y][z] = 1)  // oops!
      {
         matrix[y][z] = 0;
         if (-1 <= z < (columns - 1))
         {
            cout << y << " " << (z + 1) << " " << "E" << endl;
         }
         else
         {
            cout << y << " " << 0 << " " << "E" << endl;
         }
      }
      else
      {
         matrix[y][z] = 1;
         if (1 <= z < (columns + 1))
         {
            cout << y << " " << z - 1 << " " << "W" << endl;
         }
         else
         {
            cout << y << " " << columns - 1 << " " << "W" << endl;
         }
      }
   }
   else
   {
      if (matrix[y][z] = 1)  // oops!
      {
         matrix[y][z] = 0;
         if (-1 <= y < rows)
         {
            cout << y + 1 << " " << z << " " << "S" << endl;
         }
         else
         {
            cout << 0 << " " << z << " " << "S" << endl;
         }

      }
      else
      {
         matrix[y][z] = 1;
         if (1 <= y < (rows + 1))
         {
            cout << y - 1 << " " << z << " " << "N" << endl;
         }
         else
         {
            cout << (rows - 1) << " " << z << " " << "N" << endl;
         }
      }
   }
   for (int i = 0; i < rows; i++)
   {
      for (int j = 0; j < columns; j++)
      {
         cout << matrix[i][j];
      }
      cout << endl;
   }
   return 0;
}

The op is also trying to create a run-time sized regular array instead of a 2D std::vector.

Change line 12 (see why using code tags is a good idea?) to:
std::vector<std::vector<int>> matrix(rows, std::vector<int>(columns));

Now the code will compile using Visual Studio 2022, with warnings on lines 37, 52, 64, 79, 91, 106, 119, "'<': unsafe use of type 'bool' in operation"
Don't try and run code until you've fixed all the issues thrown up by -Wall -Wextra
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
main.cpp: In function 'int main()':

main.cpp:12:8: warning: ISO C++ forbids variable length array 'matrix' [-Wvla]

   12 |    int matrix[rows][columns];  // run-time sized regular arrays not allowed in C++

      |        ^~~~~~

main.cpp:12:8: warning: ISO C++ forbids variable length array 'matrix' [-Wvla]

main.cpp:22:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

   22 |       if (matrix[y][z] = 1)  // oops!

      |           ~~~~~~~~~~~~~^~~

main.cpp:25:16: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

   25 |          if (1 <= z < (columns + 1))

      |              ~~^~~~

main.cpp:37:17: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

   37 |          if (-1 <= z < (columns + 1))

      |              ~~~^~~~

main.cpp:49:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

   49 |       if (matrix[y][z] = 1)  // oops!

      |           ~~~~~~~~~~~~~^~~

main.cpp:52:16: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

   52 |          if (1 <= y < (rows + 1))

      |              ~~^~~~

main.cpp:64:17: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

   64 |          if (-1 <= y < (rows - 1))

      |              ~~~^~~~

main.cpp:76:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

   76 |       if (matrix[y][z] = 1)  // oops!

      |           ~~~~~~~~~~~~~^~~

main.cpp:79:17: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

   79 |          if (-1 <= z < (columns - 1))

      |              ~~~^~~~

main.cpp:91:16: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

   91 |          if (1 <= z < (columns + 1))

      |              ~~^~~~

main.cpp:103:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

  103 |       if (matrix[y][z] = 1)  // oops!

      |           ~~~~~~~~~~~~~^~~

main.cpp:106:17: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

  106 |          if (-1 <= y < rows)

      |              ~~~^~~~

main.cpp:119:16: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]

  119 |          if (1 <= y < (rows + 1))

      |              ~~^~~~

salem c wrote:
Don't try and run code until you've fixed all the issues thrown up by -Wall -Wextra

Spoilsport! :)

Seriously, once the array/vector issue is fixed the resulting logic error bugs can be a real pain in the tuchas to squash, though your compiler's warning texts are more helpful than the warnings thrown up by Visual Studio.

Yeah, using assignment '=' instead of the equality operator '==' in if comparison statements is almost as common an "oops!" as the fence post error of array indexing.
https://icarus.cs.weber.edu/~dab/cs1410/textbook/3.Control/fencepost.html
Topic archived. No new replies allowed.