sturct/pointer problem, and fscanf problem

This is a discussion on sturct/pointer problem, and fscanf problem within the C Programming forums, part of the General Programming Boards category; I'm trying to create a database of sorts that holds the deck number, name, type, etc. into a struct array. ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    sturct/pointer problem, and fscanf problem

    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");
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    /* snip */
    
    int main(void)
    {
        int menuchoice;
        int i = 0;
        int j = 1;
      
        struct pokeyman* pokey[494] = { 0 };
        int deck_number;
        FILE* fin;
        FILE* fout;
        
    /* see above
        //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)) /* http://faq.cprogramming.com/cgi-bin/...&id=1043284351 */
            {
                fscanf(fin, "%d ", &deck_number);
                printf("%d\n",deck_number);
      
    
    //getting error here for assignment for incompatible pointer type          
                pokey[deck_number] = &deck_number; /*pokey[deck_number] is a struct pokeyman*, and &deck_number is an int *. */
                fscanf(fin, "%s ", pokey[24]->name); /* what's so special about pokey[24]? */
                    
                    //prints out all pokey[] arrays that have info in them
                    for(i = 0; i < 493; i++) /* you mean 494 */
                    {
                        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;
    
    /* snip */

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    fscanf

    Thanks.

    The program keeps crashing when the fscanf tries importing the strings from my PokeyDeck.txt file. Any one see why?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What is your revised code?

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    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] = { 0 };
        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);
                pokey[deck_number] = &deck_number;
                fscanf(fin, "%s ", pokey[deck_number]->name);
                fscanf(fin, "%s ", pokey[deck_number]->type);
                fscanf(fin, "%s ", pokey[deck_number]->pokey_class);
                fscanf(fin, "%lf ", &pokey[deck_number]->weight);
            }
        }
        
        printf("+--------+--------------+----------+----------+--------+\n");
        printf("| Number | Name         | Type     | Class    | Weight |\n");
        printf("+--------+--------------+----------+----------+--------+\n");
                    
        for(i = 0; i < 493; i++)
        {
            if(pokey[i] != 0)
            {
                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");
            }
        }
        system("PAUSE");
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    the code you want me to use instead of !feof(fin) is beyond the scope of what I have learned in my programming class. Hope you understand.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > pokey[deck_number] = &deck_number;

    what are you doing here?

    > the code you want me to use instead of !feof(fin) is beyond the scope of what I have learned in my programming class. Hope you understand.

    I do understand, but unfortunately, your program will barf without it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fscanf statement
    By kuljule in forum C Programming
    Replies: 36
    Last Post: 11-02-2008, 10:56 PM
  2. problem I am having with fscanf()
    By trancekid in forum C Programming
    Replies: 2
    Last Post: 09-29-2007, 11:22 PM
  3. Replies: 12
    Last Post: 10-17-2005, 07:49 AM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 07:24 AM
  5. Problem with fscanf..
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21