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
#include <iostream>
#include <string>
#include <vector>
usingnamespace 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;
}
}
}
elseif (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;
}
}
}
elseif (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
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.
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.
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))
| ~~^~~~
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.