I'm trying to create a database of sorts that holds the deck number, name, type, etc. into a struct array. The user can enter more pokeymen, or delete them, and when the user exits, the program writes all the info in the arrays to a file. When the program is opened, it reads from the file and stores the info into the array.
When the program is reading the input file "PokeyDeck.txt" it crashes after it reads in the deck_number. Something to do with the string after it.
the txt file looks like this:
24 name type class 234.4
what am i doing wrong?
Also, I'm getting a warning when I try to set pokey[deck_number] = &deck_number
how else can this be done? I am trying to get the array position to equal the deck_number, and then set that array's position equal to the same number
i.e. if deck_number = 5
then pokey[5] = 5
but i get a warning.
Thanks
Code:
#include <stdio.h>
#include <string.h>
struct pokeyman
{
char name[13];
char type[9];
char pokey_class[9];
double weight;
};
//function prototypes
void add_pokeyman(struct pokeyman* newpokey);
int main(void)
{
int menuchoice;
int i = 0;
int j = 1;
struct pokeyman* pokey[494];
int deck_number;
FILE* fin;
FILE* fout;
//initializes pokey[] array to NULL
for(i = 0; i < 494; i++)
pokey[i] = NULL;
//opens input file and stores all info into struct array
fin = fopen("PokeyDeck.txt", "r");
if(fin != NULL)
{
while(!feof(fin))
{
fscanf(fin, "%d ", &deck_number);
printf("%d\n",deck_number);
//getting error here for assignment for incompatible pointer type
pokey[deck_number] = &deck_number;
fscanf(fin, "%s ", pokey[24]->name);
system("PAUSE");
system("PAUSE");
scanf(fin, "%s ", pokey[deck_number]->type);
system("PAUSE");
fscanf(fin, "%s ", pokey[deck_number]->pokey_class);
system("PAUSE");
fscanf(fin, "%lf ", &pokey[deck_number]->weight);
system("PAUSE");
}
}
while (menuchoice != 6)
{
//main menu for program
printf("Please choose from the following menu options:\n\n");
printf("1) Add a new Pokey Man\n");
printf("2) Delete a Pokey Man\n");
printf("3) Print information on all known Pokey Men\n");
printf("4) Search the Pokey Deck by number\n");
printf("5) Search the Pokey Deck by name\n");
printf("6) Quit\n\n");
scanf("%d", &menuchoice);
//program options 1-6
switch(menuchoice)
{
//add pokeyman
case 1:
while(j != 0)
{
printf("What is the Pokey Deck number of the Pokey Man?\n");
scanf("%d", &deck_number);
if(deck_number < 1 || deck_number > 493)
printf("That is not a valid number. Pokey Men only have "
"numbers between 1 and 493.\n\n");
else
{
j = 0;
//allocates memory for pokey[] array
pokey[deck_number] = malloc(sizeof(struct pokeyman));
add_pokeyman(pokey[deck_number]);
//pokey[deck_number] = &deck_number;
}
}
//toggles pokey deck num question back to 1 so user can re-enter
//menu option 1 and it will ask the deck num ? again.
j = 1;
break;
//delete pokeyman
case 2:
if(fin == NULL)
{
printf("\nThere are currently no Pokey Men in your file to "
"delete!\n\n");
break;
}
else
{
printf("Please enter the Pokey Deck number of the Pokey Man "
"you wish to delete.\n");
scanf("%d", &deck_number);
printf("%s has been deleted.\n",
pokey[deck_number]->name);
pokey[deck_number] = NULL;
}
break;
//print out all known pokeyman
case 3:
if(fin == NULL)
{
printf("\nThere are currently no Pokey Men in your file!\n\n");
break;
}
else
{
//why won't this work? Won't let me set pokey[deck_number] = deck_number
pokey[deck_number] = &deck_number;
printf("+--------+--------------+----------+----------+--------+\n");
printf("| Number | Name | Type | Class | Weight |\n");
printf("+--------+--------------+----------+----------+--------+\n");
for(i = 0; i < 493; i++)
{
if(pokey[i] != NULL)
{
fprintf(fout, "| %8d | %12s | %8s | %8s | %4.1lf |\n",
*pokey[i], pokey[i]->name, pokey[i]->type,
pokey[i]->pokey_class, pokey[i]->weight);
printf("+--------+--------------+----------+----------+--------+\n");
}
}
}
break;
//look up specific pokeyman by deck number
case 4:
printf("Enter the Pokey Deck number of the Pokey Man you want "
"information on.\n");
scanf("%d", &deck_number);
printf("Number: %d\n", pokey[deck_number]);
printf("Name: %s\n", pokey[deck_number]->name);
printf("Type: %s\n", pokey[deck_number]->type);
printf("Class: %s\n", pokey[deck_number]->pokey_class);
printf("Weight: %.1d\n", pokey[deck_number]->weight);
break;
//look up specific pokeyman by name
case 5:
break;
//quit program...also writes all pokeyman in arrays to PokeyDeck.txt
case 6:
fout = fopen("PokeyDeck.txt", "w");
//prints out all pokey[] arrays that have info in them
for(i = 0; i < 493; i++)
{
if(pokey[i] != NULL)
fprintf(fout, "%d %s %s %s %.1lf\n", pokey[i],
pokey[i]->name, pokey[i]->type,
pokey[i]->pokey_class, pokey[i]->weight);
}
fclose(fout);
break;
//error message when user enters a number other than 1-6
default:
printf("You can only enter a number between 1 and 6, please"
" try again\n");
}
}
system("PAUSE");
return 0;
}
//add_pokeyman function
void add_pokeyman(struct pokeyman* newpokey)
{
printf("What is its name?\n");
scanf("%s", newpokey->name);
printf("What type is it?\n");
scanf("%s", newpokey->type);
printf("What class is it?\n");
scanf("%s", newpokey->pokey_class);
printf("How much does it weigh (pounds)?\n");
scanf("%lf", &newpokey->weight);
printf("That Pokey Man was successfully aded to the Pokey Deck!\n\n\n");
}