structures, keeping high scores of users

This is a discussion on structures, keeping high scores of users within the C++ Programming forums, part of the General Programming Boards category; Write a program that allows a user to enter high scores of a game, keeping tracking of the name of ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    structures, keeping high scores of users

    Write a program that allows a user to enter high scores of a game, keeping tracking of the name
    of the user and the score. Add the ability to show the highest score for each user, all scores for a
    particular user, all scores from all users, and the list of users.

    This is the program I am trying to write, however I am having difficulty storing the users name and high score, I know I need to store an array for the score but getting confused because of the structure. (By the way I have to use structures as it is a problem from the structures chapter of the ebook I am doing). Below is my code so far. thanks.

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    struct highScore
    {
        string user;
        int score;
        int highestScore;
        int allScores;
        int allUserScores;
    
    
    };
    
    
    int main()
    {
        int entries;
        cout << "How many users would you like to enter high scores for?" << endl; //prompts user for no of entries
        cin >> entries;
        
        highScore userScore[entries];
    
    
        for(int i=0; i<entries; i++) //loop prompts user for name and score
        {
            cout << "Please enter the name for the user" << endl;
            cin >> userScore[i].user;
            cout << "Please enter the scores for " << userScore[i].user << endl;
            cin >> userScore[i].score;
        }
        for (int i = 0; i < entries; i++) //displays username and score
        {
            cout << userScore[i].user << " has the score of " << userScore[i].score << endl;
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    I'd say you should have a User structure containing name and scores array. Each time a name is entered, check the array of User structs and see if their name has come up before. If it has, add to their scores, if not, create a new User and add it to the array.
    I think the problem with your current struct is that you're trying to store too much information -- allScores, score, userScores.... too many scores. If you just store the scores alongside each user, you can construct all of that information from what you've stored.

    Doing this sort of thing in C++ is a pleasanter task than doing it in C. The C way would be to use arrays for the scores and for the users. The struct might be something like:
    Code:
     struct User
     {
        string name;
        int scores[50];
        int numScores;
     }
    A more C++ way would be to use a vector for the scores. Then the per user info would be:
    Code:
     struct User
     {
        string name;
        vector<int> scores;
     }
    You could have an array or vector of Users in the main program. Possibly better would be to use a map. Maps store values indexed by keys. In this case it'd make sense to have a map<string, User> map, with string being the name. That'd mean you wouldn't have to bother searching the array or vector for entered names. Just do:
    Code:
        map<string, User*> users;
    
        cin >> name;
        User * u = users[name];
    So yeh, there's a bunch of ways you could implement it, depending on how brave you feel. I'd probably say 'do it with arrays' - the STL containers are brilliant when you really need a dynamically growing list - but for learning about structures and arrays and stuff, a more C like version would probably be more useful.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Quote Originally Posted by smokeyangel View Post
    ...Just do:
    Code:
        map<string, User*> users;
    
        cin >> name;
        User * u = users[name];
    So yeh, there's a bunch of ways you could implement it, depending on how brave you feel. I'd probably say 'do it with arrays' - the STL containers are brilliant when you really need a dynamically growing list - but for learning about structures and arrays and stuff, a more C like version would probably be more useful.
    Two things:
    - First, the map should be map<string, User>. Not pointers. Not necessary and complicates stuff.
    - Second, don't do it with arrays. Learning how to apply standard containers is equally important to knowing the language. It's much better to prioritize learning how to apply existing stuff than to roll out your own stuff.

    >>highScore userScore[entries];
    This, btw, isn't standard C++ (yet). All arrays must have a size that is known at compile time; otherwise the program is ill-formed.
    If you must create an array whose size is not known at compile time, you should use a vector: std::vector<HighScore> UserScore(entries);
    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.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    thanks appreciate the replies. However the book has not gone through vectors or pointers yet so I think I am supposed to do without the use of them.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    A book that does not explain these things early does not do you any favours. I'd recommend you skip ahead to learning how these work before going through all the C stuff in the book (or you can skip the C stuff; it's not that relevant to modern C++, and you can learn it on demand).
    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.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    From what I can tell, you can actually process the users one at a time, so an array, vector, or pointer-based data structure isn't needed.

    Code:
    cout << "how many entries? ";
    cin >> entries;
    for (i = 0; i < entries; i++) {
      cout << "Enter name: ";
      cin >> user.name;
      cout << "enter score: ";
      cin >> user.score;
      cout << user.name << " has a score of " << user.score << endl;
    }
    I feel quite strongly that the other information, such as the highest score, isn't strictly a property of individual users. You can also find that sort of information out in the same loop. Just assume that some value is the highest, and don't change it until you encounter a bigger number than the last. When you are all done with the input routine, then you know what the highest score was.

    I think that unless you wanted to sort all of the scores or names, you wouldn't strictly need an array.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Quote Originally Posted by Elysia View Post
    A book that does not explain these things early does not do you any favours.
    Funnily enough the ebook is from Alex Allain the one who made this forum! The vectors and pointers are the next chapter so will have a read through them. Thank you for the advice.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Ah, nevermind then. Premature judgement from my side, I think.
    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.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Quote Originally Posted by whiteflags View Post
    I think that unless you wanted to sort all of the scores or names, you wouldn't strictly need an array.
    Hi thanks for the reply I think that's what the question wants. It says; Write a program that allows a user to enter high scores of a game, keeping tracking of the name of the user and the score. Add the ability to show the highest score for each user, all scores for a particular user, all scores from all users, and the list of users.
    I have done what you said to find the highest score as shown in my code below. However I am getting stuck on how to store a user name (if it comes up more than once) with a different high score. That's where I think I need an array.

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    struct User
    {
        string name;
        int score;
        //int numScores;
    };
    
    
    int main()
    {
    
    
        User user;
    
    
        int entries;
    
    
        cout << "How many entries?" << endl;
        cin >> entries;
    
    
        int big;
        for(int i=0; i < entries; i++)
        {
          cout << "Please enter the name for the user" << endl;
          cin >> user.name;
          cout << "Please enter the score for " << user.name << endl;
          cin >> user.score;
          cout << user.name << " has a score of " << user.score << endl;
        }
        cout << endl;
        big=user.score;
        if (user.score<big)
        {
          big = user.score;
        }
        cout << "The highest score is " << big << endl;
    
    
        return 0;
    }

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes you need an array.
    you have to dynamically allocate it like

    Code:
    User * scores = new User[entries];
    in the loop you read the name like
    Code:
    cin >> scores[i].name;
    don't forget to delete [] scores at the end of the program

    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Low Game with High Scores
    By Bradley Buck in forum C Programming
    Replies: 24
    Last Post: 05-27-2011, 01:42 PM
  2. Keeping high scores for pong
    By trev456 in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2007, 05:44 AM
  3. Help on high scores
    By Gnoober in forum C++ Programming
    Replies: 0
    Last Post: 02-17-2003, 07:28 PM
  4. Working on High Scores...
    By Gnoober in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2003, 12:50 PM
  5. High Scores
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2003, 01:08 PM

Tags for this Thread


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