I'm trying to take from stdin in a format like this
3
0 -1 1 -1 2
2 0 -1 -1 -1
2 -1 -1 0 -1
4
1 0
1 2
2 2
0 1
and create structs and fill in the states based on that information Whenever I enter in these inputs it seems to stop whenever it sees a 0 or a 2 I have no idea whats causing this Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void getInput();
int getNrOfX();
int getCurrentRoom();
struct Room {
int state;
int northState;
int southState;
int eastState;
int westState;
};
struct Creature {
int type;
int location;
};
int main() {
//creates all the inital rooms and creatures
int nrOfRooms = getNrOfX();
struct Room rooms[nrOfRooms];
for(int i = 0; i < nrOfRooms; i++){ //creates rooms and stores them into the rooms array
scanf("%d %d %d %d %d", rooms[i].state, rooms[i].northState, rooms[i].southState, rooms[i].eastState, rooms[i].westState);
}
int nrOfCreatures = getNrOfX();
struct Creature creatures[nrOfCreatures];
for(int i = 0; i < nrOfCreatures; i++){ //creates creatures and stores them in the creatures array
scanf("%d %d", creatures[i].type, creatures[i].location);
}
//Gameplay loop
while (1) {
char input[30];
scanf("%s", input);
if(strcmp(strupr(input), "LOOK") == 0) {
int playerLocation = getCurrentRoom(nrOfRooms, &creatures);
printf("Room State %d\n", rooms[playerLocation].state);
printf("%d\n", rooms[playerLocation].northState);
break;
}
}
return 0;
}
int getNrOfX() {
int nrOfX;
scanf("%d", &nrOfX);
return nrOfX;
}
int getCurrentRoom(int nrOfRooms, struct Creature *creature) {
for(int i = 0; i < nrOfRooms; i++) {
if (creature[i].type == 0){
return creature[i].location;
}
}
}
The program compiles but when I enter the input
2 0 -1 -1 -1
ParserError:
Line |
1 | 2 0 -1 -1 -1
| ~
| Unexpected token '0' in expression or statement.
Please let me know what I can do