Stuck in vector excercise!

This is the math:
Use vectors to implement a high score list for a video game. Make it so that scores are updated
automatically, and new scores are added into the right place in the list.

Problem is my program cann't add more than 5 elements to the list, and it's not in order as i expect. Blow up my mind and still got stuck. plz help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>

using namespace std;

void displayScore(vector<int>);
int addScore(vector<int> *, int);

main()
{
    vector<int> listScore(1);
    int choice, inputScore;
    listScore[0] = 0;                   //initial first value 0 to use in addScore function
    while (1)
    {
        cout << "Your choice?\n" << "1.New score\t" << "2.View scores\t" << "3.Quit\n> ";
        cin >> choice;
        if (choice == 1)               //get the score and use addScore to put in the right position in the list
        {
            cout << "Input score: ";
            cin >> inputScore;
            addScore(&listScore, inputScore);
        }
        else
            if (choice == 2)
            {
                displayScore(listScore);
            }
            else
                if (choice == 3)
                {
                    break;
                }
                else
                   {
                        cout << "Wrong choice!\n";
                   }
    }
}

int addScore(vector<int> *listScore, int inputScore)
{
    vector<int>::iterator itr = (*listScore).begin();
    if (*itr == 0)
    {
        *itr = inputScore;
        return 0;
    }
    if (inputScore < *(itr = (*listScore).end()))
    {
        (*listScore).push_back(inputScore);
        return 0;
    }
    for (itr = (*listScore).begin(); itr != (*listScore).end(); itr++)   //if input score is bigger than the current score,
    {                                                                   //put the input in front of it
        if (inputScore >= *itr)
        {
            (*listScore).insert(itr, inputScore);
        }
    }
    return 0;
}
void displayScore(vector<int> listScore)
{
    int rankScore = 1;
    for (vector<int>::iterator itr = listScore.begin(); itr != listScore.end(); itr++)
    {
        cout << rankScore++ << ". " << *itr << endl;
    }
}
Last edited on
Why do you use a separate addScore() function?
Simply add the score with push_back(), then sort the vector. For sorting it, use std::sort() from algorithm.

http://cplusplus.com/reference/algorithm/sort/

1
2
3
4
5
#include <algorithm>

// ...

std::sort(listScore.begin(), listScore.end());


This said, are you required to use std::vector?
Because I think a better choice in your case would be std::multiset.

http://cplusplus.com/reference/set/multiset/
Last edited on
Ok, i'll try it, thank!

But do you know why my list can only have 5 item, and why the (*listScore).insert(itr, inputScore); doesn't put inputScore in front of itr
http://cplusplus.com/reference/vector/vector/insert/#validity

Iterator validity

If a reallocation happens, all iterators, pointers and references related to the container are invalidated.
Otherwise, only those pointing to position and beyond are invalidated, with all iterators, pointers and references to elements before position guaranteed to keep referring to the same elements they were referring to before the call.


It is possible, in the code below, that insert() will invalidate the iterator itr.

General advice: do not insert new elements inside a for() loop where you iterate the original elements.

1
2
3
4
5
6
7
    for (itr = (*listScore).begin(); itr != (*listScore).end(); itr++)   //if input score is bigger than the current score,
    {                                                                   //put the input in front of it
        if (inputScore >= *itr)
        {
            (*listScore).insert(itr, inputScore);
        }
    }
appreciate your help very much! ^_^. now i run just fine, tk
Topic archived. No new replies allowed.