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);
}
}
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments