In: Computer Science
Hi guys,
I need someone to look over a some codes. My xcoder telling I have error(s), and I need someone to look what causing these errors.
I'm leaving the codes that are giving me errors. I don't want to give my whole code away for privacy reasons. Language is C
1.
#include
#include
#include
#define MAXCHAR 50
/*Here are the structs for the monsters, regions, and commonality.*/
/*typedef struct was provide in instruction*/
typedef struct monster{
int id;
char *name;
char *element;
int popultaion;
}monster;
typedef struct region{
char *name;
int nmonsters;
int total_population;
monster **monsters;
}region;
typedef struct itinerary{
int nregion;
region **region;
int captures;
}itinerary;
typedef struct trainer{
char *name;
itinerary *visits;
}trainer;
/*Monster fill informations. Constructor. */
monster* newCreate( char *name, int popultaion, char *element){
int gameNam, loveGame;
/*Assigns string to a variable for ease of use.*/
gameNam = strlen(gameNam) + 1; <------------Here is my error
loveGame = strlen(loveGame) + 1;<------------Here is my error
/*Malloc new space.*/
monster *newCreature = (struct monster*)malloc(sizeof(struct monster));
newCreature->name = (char*)malloc(gameNam * sizeof(char));
newCreature->element = (char*)malloc(loveGame * sizeof(char));
/*All struct values will be assigned*/
strncpy(newCreature->name, name, gameNam);
strncpy(newCreature->element, element,loveGame);
newCreature->popultaion = popultaion;
return newCreature;
}
2.
monster** readCreatures(FILE* inFile, int *numbers){
monster **createL, *monster;
char name[MAXCHAR], element[MAXCHAR];
int population;
fscanf(inFile, "%d%*s", numbers);
createL = (struct monster**)malloc(*numbers * (sizeof(struct monster)));
/*This will allow the list to be filled.*/
for(int i=0; i < * numbers; i++){
fscanf(inFile, "%s %s %d", name, element, &population);
creatureList[i] = createL(name, element, population);<------------Here is my error
}
return createL;
}
------------------------
Input file:
8 monstersStAugustine
Grass 12
Zoysia Grass 8
WholeWheat Bread 6
MultiGrain Bread 10
Rye Bread 10
Cinnamon Spice 5
Pepper Spice 10
Pumpkin Spice 303 regions
Rome4 monsters
The errors in the code provided above are due to follwoing reasons:
Error 1 and Error 2:
gameNam = strlen(gameNam) + 1; <------------Here is my error
loveGame = strlen(loveGame) + 1;<------------Here is my error
These both are errors due to the same reason
that both variables used are integer
int gameNam, loveGame;
Now we have used strlen function
so first check syntax of strlen function is
size_t strlen( const char* str );
which we can clearly see takes as arguement as character pointer or a string
and returns size_t which is a integer
Probable Error if int is passed instead of char *
initializing argument 1 of 'size_t strlen(const char*)'
extern size_t strlen (const char *__s)
Mistake:
and you have used as parameter gameNam and loveGame which are both integer thus this will give you error as this will not be found
so character pointer or string should be passed as parameter
I think in the function char *name is passed as parameter so according to your logic modify code
and it could be used as
gameNam = strlen(name) + 1;
which will give length of name added to 1 so name="abcd"
gameNam woud be 4+1=5
For error 3
creatureList[i] = createL(name, element, population);<------------Here is my error
Mistake:
The reason of error is that for the file you have not done error checking procedure while taking from file that is the number present in file or not and for that also check defination of createL defination. For privacy reason as you have not uploaded full solution so please check for signature of class.
Like in last line Rome4 monsters can cause error if not extrcted properly
like is the parameter order same with correct type and defination also implement error checking while extracting input form the file
Hope you are able to understand the concepts and remove errors