0

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

  • Does this answer your question? [scanf crashes when trying to read a float into an array](https://stackoverflow.com/questions/14666263/scanf-crashes-when-trying-to-read-a-float-into-an-array) – phuclv Feb 08 '23 at 16:14
  • you need `&` => `scanf("%d %d %d %d %d", &rooms[i].state, &rooms[i]...` – phuclv Feb 08 '23 at 16:15
  • That error means you tried to use the input as a program to compile. You need to run your program and then enter the input. – Barmar Feb 08 '23 at 17:06

1 Answers1

0

I have no idea whats causing this

Save time, enable all warnings

Enable the compiler to fully do its job.

format '%d' expects argument of type 'int *', but argument 6 has type 'int' [-Wformat=]
scanf("%d %d %d %d %d", rooms[i].state, rooms[i].northState, rooms[i].southState, rooms[i].eastState, rooms[i].westState);

With scanf(), "%d" matches an int *, not an int.

    // scanf("%d %d %d %d %d", rooms[i].state, rooms[i].northState, rooms[i].southState, rooms[i].eastState, rooms[i].westState);
    scanf("%d %d %d %d %d", &rooms[i].state, &rooms[i].northState, 
        &rooms[i].southState, &rooms[i].eastState, &rooms[i].westState);

Same for scanf("%d %d", creatures[i].type, creatures[i].location);.

In function 'getCurrentRoom':
:1: warning: control reaches end of non-void function [-Wreturn-type]

getCurrentRoom() does not always return a value.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256