Thread: Question about struct

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    Question about struct

    I've created my structure and now I'm trying to use a couple loops to sort through the data.

    struct stats myPlayers[numPlayers]; // this is the struct

    Code:
    int sortStats(int numPlayers, struct stats myPlayers)
    {
        int mostGoals = 0;
        int mostGoalsInd = 0;
        for (int i=0;i<numPlayers;i++)
        {
            if (myPlayers[i].goals > mostGoals)
            {
                 mostGoals = myPlayers[i].goals;
                 mostGoalsInd = myPlayers[i].ind;
            }
        }
        printf("%s scored the most goals this season with %d.\n", myPlayers[mostGoalsInd], mostGoals);
    
        return 0;
    }
    This is the function I've created to sort through the stats, as you my have guessed by the name. I want the function to sort through each member and for this example, determine which player has scored the most goals.

    I'm getting an error at the if statement. I believe it's because it's not recognizing myPlayers[i].whatever. I think I need to have some sort of initialization to tell it where to pull from, which I thought is what I did when putting the struct stats myPlayers in the braces for the sortStats function. Where do you think I'm going wrong?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have a doubt.
    The printf is way off. It's a struct, so you must pick a member to print, no?
    Last edited by Elysia; 12-07-2008 at 01:20 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    The printf is off, it was from a previous way I was trying to do something. I've commented it out.

    The error at the if statement is the same for 3 lines, the line with the if statement, the line with the redefining mostGoals and the line redefining mostGoalsInd. The error is "subscripted value is neither array nor pointer"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course!
    You are passing a struct, not several, so there is no array to talk of!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    int sortStats(int numPlayers, struct stats myPlayers[numPlayers])
    or
    Code:
    int sortStats(int numPlayers, struct stats* myPlayers)
    edit: This forum sure has some damn good support. It is not the first time I post something right before/after somebody else

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Hah! Thanks.

    Now, when I call this function in main, I'm not sure what to put as the second argument.

    sortStats(numPlayers, ??)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pass the name of the array.
    Do not pass a array (ie myarray[n]), but the name of the array itself (ie myarray).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Ok, so I would want to pass myPlayers in this instance, no?

    Except myPlayers isn't defined in main. The structure is built in a function loadData. I tried declaring myPlayers and saying myPlayers = loadData(numPlayers, myStr) where myStr is the fileName, but it doesn't like that.

    Sorry for having so many questions, but I sure do appreciate the help and timeliness

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    This is the whole program as of right now, might give you more of an idea how I have it setup. I'm a bit new to all of this so it's probably not the best setup, but it's what I've been taught thus far and how my thought process is working at this point.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int ind, gamesPlayed, goals, assists, points, plusMinus, penaltyMins, ppGoals, shGoals, gwGoals, otGoals, shots;
    float shotPercent, shiftsPerGame, faceOffWinPercent;
    char player[50];
    char team[4];
    char pos[2];
    char timeOnIce[10];
    int strPlayer = 0;
    int strTimeOnIce = 0;
    
    
    int getNumPlayers(char *fileName)
    {
       int numPlayers = 0;
       FILE *fp;
       fp = fopen(fileName, "r");
       while (!feof(fp))
       {
            fscanf(fp, "%d %s %s %c %d %d %d %d %d %d %d %d %d %d %d %f %s %f %f", &ind, player, team, &pos, &gamesPlayed, &goals, &assists, &points, &plusMinus, &penaltyMins, &ppGoals, &shGoals, &gwGoals, &otGoals, &shots, &shotPercent, timeOnIce, &shiftsPerGame, &faceOffWinPercent);     
            numPlayers++;
       }
       printf("Number of players: %d\n", numPlayers+1); //add 1 to account for starting point of 0
       fclose(fp);
       return numPlayers;
    }
    
    struct stats
    {
           int ind;
           char player[25];
           char team[4];
           char pos[2];
           int gamesPlayed;
           int goals;
           int assists;
           int points;
           int plusMinus;
           int penaltyMins;
           int ppGoals;
           int shGoals;
           int gwGoals;
           int otGoals;
           int shots;
           float shotPercent;
           char timeOnIce[6];
           float shiftsPerGame;
           float faceOffWinPercent;
    };
    
    
    
    int getLength(int numPlayers, char *fileName)
    {
        FILE *fp;
        fp = fopen(fileName, "r");
        while (!feof(fp))
       {
            fscanf(fp, "%d %s %s %c %d %d %d %d %d %d %d %d %d %d %d %f %s %f %f", &ind, player, team, &pos, &gamesPlayed, &goals, &assists, &points, &plusMinus, &penaltyMins, &ppGoals, &shGoals, &gwGoals, &otGoals, &shots, &shotPercent, timeOnIce, &shiftsPerGame, &faceOffWinPercent);
            if (strlen(player) > strPlayer) strPlayer = strlen(player);
            if (strlen(timeOnIce) > strTimeOnIce) strTimeOnIce = strlen(timeOnIce);    
       }
        printf("The longest player name is %d characters long\n", strPlayer);
        printf("The time on ice vector needs %d characters\n", strTimeOnIce);
        fclose(fp);
        return 0;
    }
    
    int sortStats(int numPlayers, struct stats myPlayers[numPlayers])
    {
        int mostGoals = 0;
        int mostGoalsInd = 0;
        int i = 0;
        for (i=0;i<numPlayers;i++)
        {
            if (myPlayers[i].goals > mostGoals)
            {
                 mostGoals = myPlayers[i].goals;
                 mostGoalsInd = myPlayers[i].ind;
            }
        }
        //printf("%s scored the most goals this season with %d.\n", myPlayers[mostGoalsInd], mostGoals);
    
        return 0;
    }
    
    int loadData(int numPlayers, char *fileName)
    {
        int i = 0;
        FILE *fp;
        fp = fopen(fileName, "r");
        struct stats myPlayers[numPlayers];
             while (!feof(fp))
             {
                fscanf(fp, "%d %s %s %c %d %d %d %d %d %d %d %d %d %d %d %f %s %f %f", &ind, player, team, &pos, &gamesPlayed, &goals, &assists, &points, &plusMinus, &penaltyMins, &ppGoals, &shGoals, &gwGoals, &otGoals, &shots, &shotPercent, timeOnIce, &shiftsPerGame, &faceOffWinPercent);
                myPlayers[i].ind = ind;
                strcpy(myPlayers[i].player, player);
                strcpy(myPlayers[i].team, team);
                strcpy(myPlayers[i].pos, pos);
                myPlayers[i].gamesPlayed = gamesPlayed;
                myPlayers[i].goals = goals;
                myPlayers[i].assists = assists;
                myPlayers[i].points = points;
                myPlayers[i].plusMinus = plusMinus;
                myPlayers[i].penaltyMins = penaltyMins;
                myPlayers[i].ppGoals = ppGoals;
                myPlayers[i].shGoals = shGoals;
                myPlayers[i].gwGoals = gwGoals;
                myPlayers[i].otGoals = otGoals;
                myPlayers[i].shots = shots;
                myPlayers[i].shotPercent = shotPercent;
                strcpy(myPlayers[i].timeOnIce, timeOnIce);
                myPlayers[i].shiftsPerGame = shiftsPerGame;
                myPlayers[i].faceOffWinPercent = faceOffWinPercent;
                i++;
             }
                return 0;
    }
    
    
    int main(int argc, char *argv[])
    {
      int numPlayers;
      char myStr[] = "nhl0708.txt";
      char myPlayers[1000];
      numPlayers = getNumPlayers(myStr);
      getLength(numPlayers, myStr);
      myPlayers = loadData(numPlayers, myStr);
      sortStats(numPlayers, myPlayers);
      system("PAUSE");
      return 0;
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot return an array, but you can pass an array.
    If both functions need it, define it is the most common scope you can find.
    Since main calls both functions, have main define it and pass it along as arguments to both functions.
    One can fill the struct, and one can use it.

    Are you making a game, perchance?
    Have you ever thought about C++? It is, after all, the preferred language for games.

    Also, you have far too many globals. Globals are bad, because they can easily created bugs and make code a mess.
    Move them inside main and pass them around as arguments whenever necessary.
    At least, I think you should avoid globals for now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    No, not making a game, not trying to at least, maybe it could be looked at in that light.

    Have a class that was supposed to be about C++, started out spending the better part of 12 weeks learning Matlab, a couple on C, and maybe 3 lectures on C++. Have a project to find a "neat data set", and ask some "neat questions" and have the program compute the answers. Can be in C or C++, but since we've had maybe 4 hours spent on C++, I don't know enough about the difference between C and C++ to really be able to write anything useful using that...and apparently I don't know a whole lot about C either, or just haven't practiced enough...either way...

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I know globals aren't the best idea, but I didn't think it would have been a problem for this program. Removed them anyhow, and just defined them in each function they were being called in.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, that's completely alright. It's not a huge deal. You can learn other languages at a later date, if you wish.
    I wish just thinking if you wanted to write a game, then C++ might be better since most games are written in C++.
    It's best to prioritize one language first! Since you've already started with C, you might as well continue, until you have an idea of what you want to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Quote Originally Posted by Elysia View Post
    You cannot return an array, but you can pass an array.
    If both functions need it, define it is the most common scope you can find.
    Since main calls both functions, have main define it and pass it along as arguments to both functions.
    One can fill the struct, and one can use it.
    Back to this...

    We didn't spend much time on using define. And looking a source I've been trying to use as reference, I see there's a way to define structures using something like this
    Code:
      typedef struct {
              char name[25];
              char team[5];
              int goals;
              int assists;
              .
              .
              .
      } myPlayers;
    Is this what you're referring to me doing in main? And would that be setup correctly like that if that is indeed what you're referring to?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Put that struct outside main (typically in a header), first.

    Code:
    int main()
    {
        Player player[n];
        Function1(player, sizeof(player), ...);
        Function2(player, sizeof(player), ...);
    }
    And remember that the typedef "renames" the struct, from anonymous to myPlayers. It does not create an instance of it, so you probably want to name it Player or something.

    Now that you are passing player to the functions, they can do the work on them, and the work carries on as you pass it to the next function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM