I am trying to convert a given program from using c-string array and a dynamic array to a vector. The program asks the user to enter how many scores they would like to enter, the name for each score, the value of each score, and then prints them in descending order.I have some confusion because the instructions say that the struct must still be a c-string, and I am not seeing how that serves a purpose if the program is to convert to a vector. Also, I am currently trying to get the vector size from user data as mentioned by the last paragraph below. I do not understand how to invoke the STL vector constructor, and I do not have any specific examples in my textbook about this so I am not sure what I am missing.
Instructions:
Here is a high scores program. Rewrite this program using an STL vector instead of an array. (Note, you will be using an STL vector, not the MyVector class developed in lesson 19.)
Clarifications and Additional Requirements:
Documentation is not required for this assignment.
Your program must use three functions that accept the vector of Highscore structs (the size parameter from the given code won't be needed now, since a vector knows its own size). You must use these function headers:
void readData(vector<Highscore>& scores)
void sortData(vector<Highscore>& scores)
void displayData(const vector<Highscore>& scores)
The name field in the struct must still be a c-string
The focus of this assignment is to use iterators. You must use iterators wherever possible to access the vector. As a result, you must not use square brackets, the push_back() function, the at() function, etc. You won't get full credit if you miss an opportunity to use iterators.
You should still ask the user to enter the number of scores there will be, and then you should create a vector with the required capacity. You can do this by using the vector class's constructor that creates a vector with the capacity indicated by its parameter. For example, to create a vector of size 100, use this:
vector<sometype> myExampleVector(100);
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
|
#include <iostream>
#include <vector>
using namespace std;
const int MAX_NAMESIZE = 24;
struct Highscore {
char name[MAX_NAMESIZE];
//int score;
};
void getArraySize(vector<Highscore>& scores);
/*
void readData(vector<Highscore>& scores);
void sortData(vector<Highscore>& scores);
//int indexOfLargest(const Highscore highScores[], int startingIndex, int size);
void displayData(const vector<Highscore>& scores);
*/
int main()
{
/*
Highscore* highScores;
int size;
*/
vector<Highscore> scores;
getArraySize(scores);
/*
highScores = new Highscore[size];
readData(highScores, size);
sortData(highScores, size);
displayData(highScores, size);
delete[] highScores;
*/
}
void getArraySize(vector<Highscore>& scores)
{
cout << "How many scores will you enter?: ";
vector<Highscore> scores;
}
|