ProgrammingC Switch only executes one specific case every iterationPosted:

3PT
  • 1K Rainmaker
Status: Offline
Joined: Oct 28, 20133 Year Member
Posts: 1,044
Reputation Power: 90
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:
r00t
  • Retired Staff
Status: Offline
Joined: May 18, 20116 Year Member
Posts: 15,444
Reputation Power: 1173
Motto: thankyouillsaygoodbyenowthoughitstheendoftheworlddontblameyourselfandifitstrueiwillsurroundyouandgivelifetoaworldthatsourown
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
#3. Posted:
tortuga
  • Wizard
Status: Offline
Joined: Dec 25, 20097 Year Member
Posts: 2,305
Reputation Power: 513
r00t wroteWhen 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.
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS