Question

I'm working on my latest new function. Any explanations would be great since I'm new to programming. I couldn't figure out how to fix this because the errors I got are: 18 syntax error before '{' token

20 case label not within a switch statement

21 (same as above)

22 (same as above)

23 `default' label not within a switch statement

29 [Warning] `return' with a value, in function returning void

32 [Warning] assignment makes pointer from integer without a cast

34 [Warning] `return' with a value, in function returning void

void moveCar(char board[], char vehicle, char direction, int distance)
{
     int i;
     int position, newPosition;
     int offset;
     for (i = 0; i < size; i++) //the main loop for the vehicles and user's input
     {
         if(isalpha(vehicle))//vehicles for all letters but 'x'
         { 
             if(board[i] == vehicle)//vehicles on board
             {
                  swtich(direction)
                  {
                         case 'r': offset = 1; break;
                         case 'l': offset = -1; break;
                         case 'u': offset = -8; break;
                         case 'd': offset = 8; break;
                         default: printf("invalid direction"); break;

                         newPosition = position + offset; 
                         if(newPosition != '.')
                         {
                                         printf("invalid move.");
                                         return 0;
                         }

                         board = '.';
                         board[newPosition] = vehicle; 
                         return 1;
                   }      
               }
          }
}
Was it helpful?

Solution

It looks like you have a typo of Switch as Swtich, the compiler won't recognize it, and the case statements will fail to compile.

Additionally you close your switch statement too early. If you want those commands to execute only in default move them before the break. If you want them to execute for every case move them outside of the switch statement entirely

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top