0

I'm creating a game that creates a struct of creatures and struct of rooms using malloc. In my first function, i create the amount of rooms that the user inputs. I then ask the user for the status of the room either 1 2 or 3 then ask the cords for north south east and west. That is all for this function. Everything is fine up to this point. Then when i create my creatures, I initialize them through input via user. I ask the user for the creature type which can only be 0 1 or 2, then ask for the location of the creature and the location will be associated with the room number. So if creature location is 1 then its in room 1. but for some reason it changes my cords in my rooms in the creature function. Literally changes them out of no where.

Example, I enter for 4 rooms, first room 0,1,2,3,4 then second room 3,1,2,4,3 then same for room 3 and four. For now, the cords dont matter but my problem is that through the creature function, it changes my cords for some reason. Can someone please help me. I know this is a lot of code but I'm out of ideas

struct room
{
   int roomNum;
   int creaturesTotal;
   int roomStatus;
   int roomTotal;
   int north;
   int south;
   int east;
   int west;
};

struct Creatures
{
  int creatureType;
  int creatureNum;
  int location;
};

 int main()
{
int numberofrooms = 0;
int numberofcreatures = 0;


/*ask user for rooms and creatures*/
printf("How many rooms? Max 10 rooms: ");
scanf("%d",&numberofrooms);
/*make sure its under 10 rooms*/
while(numberofrooms > 10)
{
    printf("\nToo many rooms!\n");
    printf("How many rooms? Max 10 rooms: ");
    scanf("%d",&numberofrooms);
}

printf("How many creatures? Max 100 creatures: ");
scanf("%d",&numberofcreatures);

while(numberofcreatures > 100)
{
    printf("\nToo many creatures! MAX 100 creatures please!\n");
    printf("How many creatures? Max 100 creatures: ");
    scanf("%d",&numberofcreatures);
}

  struct Creatures*AllCreatures = malloc(numberofcreatures * sizeof(numberofcreatures));
  struct room *AllRooms = malloc(numberofrooms * sizeof(numberofrooms));

  createRooms(numberofrooms, AllRooms);
  createCreatures(numberofcreatures,AllCreatures,AllRooms);
 }

void createCreatures(int numberofcreatures, struct Creatures* AllCreatures,struct room* AllRooms)
{
  int location;

  int counter = 0;
  int PC = 0;

//ask the user for creatures and check the inputs
for(int i=0; i < numberofcreatures; i++)
{
    int creatureType;

        printf("\nType of Creature, Location: ");
        scanf("%d%d",&creatureType,&location);

        //if room is full
        while(AllRooms[location].roomTotal == 10)
        {
            printf("\nRoom is already full!\n");
            printf("\nType of Creature, Location: ");
            scanf("%d%d",&creatureType,&location);

            //make sure isnt invalid creature num in nested while loop
            while(creatureType < 0 || creatureType > 2)
            {
                printf("\ninvalid creature type\n");
                printf("\nType of Creature, Location: ");
                scanf("%d%d",&creatureType,&location);
            }
        }

        //if room isnt full but invalid creature type
        while(creatureType < 0 || creatureType > 2)
        {
            printf("\ninvalid creature type\n");
            printf("\nType of Creature, Location: ");
            scanf("%d%d",&creatureType,&location);
        }

         if(creatureType == 0)
         {
             PC++;
             while(PC > 1)
            {
                 printf("\nThere is already a PC player, enter again");
                 printf("\nType of Creature, Location: ");
                 scanf("%d%d",&creatureType,&location);
                 if(creatureType == 1 || creatureType == 2)
                 {
                     PC--;
                 }
            }
         }


    //print out the creatures with the room numbers
    AllCreatures[i].location = location;
    AllCreatures[i].creatureType = creatureType;
    AllCreatures[i].creatureNum = counter;

    //AllRooms[AllCreatures[i].location].roomTotal = AllRooms[AllCreatures[i].location].roomTotal + 1;
    counter++;
}

for(int i=0; i < numberofcreatures; i++)
{
    printf("\n Creature num %d, type %d, location %d\n",AllCreatures[i].creatureNum, AllCreatures[i].creatureType,AllCreatures[i].location);
}
}

//create all rooms
void createRooms(int numberofrooms,struct room* AllRooms)
{
   int counter = 0;
   int status;
   int north;
   int south;
   int east;
   int west;

//ask the user for the cords
for(int i =0; i < numberofrooms;i++)
{
    printf("Room Number %d state north south east west: ",counter);
    scanf("%d%d%d%d%d",&status,&north,&south,&east,&west);
    AllRooms[i].roomStatus = status;
    AllRooms[i].north = north;
    AllRooms[i].south = south;
    AllRooms[i].east = east;
    AllRooms[i].west = west;
    AllRooms[i].roomNum = counter;
    AllRooms[i].roomTotal = 0;
    counter++;
}

//print out the cords
for(int i =0; i < numberofrooms;i++)
{
    printf("\n%d,%d,%d,%d,%d\n",AllRooms[i].roomStatus,AllRooms[i].north,AllRooms[i].south,AllRooms[i].east,AllRooms[i].west);
}
}
3
  • Your mallocs have the wrong number of bytes; Try this pattern: T *p = malloc(N * sizeof *p); Commented Feb 14, 2020 at 4:25
  • Also you call undeclared functions in main , the function declaration must appear earlier than the call. The compiler should warn about this -- adjust your settings if you dont' see a warning Commented Feb 14, 2020 at 4:26
  • Im sorry. i forgot to add the declaration in my question. But thank you so much, it seems that i did have the wrong number of bytes. I would of never guessed that was wrong. Thank you so much! Commented Feb 14, 2020 at 4:32

1 Answer 1

2

Malloc Error Not sure if this is the cause of the problem, but you aren't allocating enough space.

Current: struct Creatures*AllCreatures = malloc(numberofcreatures * sizeof(numberofcreatures)); struct room *AllRooms = malloc(numberofrooms * sizeof(numberofrooms));

Change to: struct Creatures*AllCreatures = malloc(numberofcreatures * sizeof(struct Creatures)); struct room *AllRooms = malloc(numberofrooms * sizeof(struct room));

This could be the problem, but if it doesn't fix it comment on this and I will red more to see if I can solve it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your comment, it seems like changing my mallocs to this format worked. T *p = malloc(N * sizeof *p);
I guess i didnt have the right number of bytes and someone that was changing my numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.