### 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.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include #include using namespace std; void displayScore(vector); int addScore(vector *, int); main() { vector 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 *listScore, int inputScore) { vector::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 listScore) { int rankScore = 1; for (vector::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/

 ``12345`` ``````#include // ... 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.

 ``1234567`` `````` 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.