You are viewing our Forum Archives. To view or take place in current topics click here.
C Switch only executes one specific case every iteration
Posted:
C Switch only executes one specific case every iterationPosted:
Status: Offline
Joined: Oct 28, 201311Year Member
Posts: 1,048
Reputation Power: 91
void placeAircraftCarrier(char battleField[11][11]) {
int x, y, z, i, temp;
char collision;
srand((unsigned)time(NULL));
// test if ship placement is possible
do {
collision = 'N';
x = (rand() % 9) + 1;
y = (rand() % 9) + 1;
z = (rand() % 4) + 1;
printf("case %i was selected.\n", z);
if (z = 1) {
temp = x;
for (i = 0; i < 5; i++) {
if (battleField[temp][y] != 'W') {
collision = 'Y';
break;
}
temp++;
}
}// end if case 1
else if (z = 2) {
temp = x;
for (i = 0; i < 5; i++) {
if (battleField[temp][y] != 'W') {
collision = 'Y';
break;
}
temp--;
}
}// end if case 2
else if (z = 3) {
temp = y;
for (i = 0; i < 5; i++) {
if (battleField[x][temp] != 'W') {
collision = 'Y';
break;
}
temp++;
}
}// end if case 3
else if (z = 4) {
temp = y;
for (i = 0; i < 5; i++) {
if (battleField[x][temp] != 'W') {
collision = 'Y';
break;
}
temp--;
}
}// end if case 4
} while (collision != 'N');
// place the ships
switch (z) {
case 1:// down
printf("1\n");
for (i = 0; i < 5; i++) {
battleField[x][y] = 'A';
x++;
}
break;
case 2:// up
printf("2\n");
for (i = 0; i < 5; i++) {
battleField[x][y] = 'A';
x--;
}
break;
case 3:// right
printf("3\n");
for (i = 0; i < 5; i++) {
battleField[x][y] = 'A';
y++;
}
break;
case 4:// left
printf("4\n");
for (i = 0; i < 5; i++) {
battleField[x][y] = 'A';
y--;
}
break;
default:
PAUSE;
}// end switch
displayGrid(battleField);
}// end placeAircraftCarrier
This is a function in my battleship program I'm writing. First it generates the coordinates of the first plot of a ship and then generates a case in that will determine the direction it faces on the game board. However when I compile everything, it only executes case 1 even if the 'z' variable (which controls the switch) says otherwise. I'm not too sure what the problem is and any help would be appreciated.
#2. Posted:
Status: Offline
Joined: May 18, 201113Year Member
Posts: 16,419
Reputation Power: 24469
Status: Offline
Joined: May 18, 201113Year Member
Posts: 16,419
Reputation Power: 24469
if (z = 1)
This is assigning the value of 1 to variable z. When an this (see tortuga's post) assignment statement is evaluated for truth, it will be TRUE.
You need to test for equality, so use the equality operator:
if (z == 1)
Last edited by r00t ; edited 1 time in total
- 2useful
- 1not useful
#3. Posted:
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
r00t wrote When an assignment statement is evaluated for truth, it will be TRUE.This is not necessarily true. Assignment typically evaluates to the left operand so that `if (z = 0)` is equivalent to `if (0)`, which will be interpreted as false in C. This is also why we can do things like `a = b = c = 1` in most C-like languages.
- 1useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.