Vectors

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;
}


Last edited on
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

struct name_and_score {

    static const int MAX_NAMESIZE = 24;

    char name[MAX_NAMESIZE] = "" ;
    int score = 0 ;
};

void readData( std::vector<name_and_score>& scores ) {

    // You must use iterators wherever possible to access the vector.
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto iter = scores.begin() ; iter != scores.end() ; ++iter ) {

        // the iterator is a pointer-like entity.
        // iter->name: the name member of the object 'pointed to' by the iterator etc.

        std::cout << "\nname (max " << sizeof(iter->name)-1 << " characters, no spaces): " ;
        std::cin >> std::setw( sizeof(iter->name) ) >> iter->name ;
        std::cin.ignore( 1000, '\n' ) ; // throw the rest of this input line away
                                        // (the user may have entered too many characters)

        std::cout << "score: " ;
        std::cin >> iter->score ;
    }
}

// helper function to compare scores (for sorting in descending order of scores)
bool compare_scores( const name_and_score& a, const name_and_score& b ) {

    return a.score > b.score ; // higher scores appear earlier
}

void sortData( std::vector<name_and_score>& scores ) {

    // http://www.cplusplus.com/articles/NhA0RXSz/
    std::sort( scores.begin(), scores.end(), compare_scores ) ;
}

void displayData( const std::vector<name_and_score>& scores ) { // note: const

    // You must use iterators wherever possible to access the vector.
    for( auto iter = scores.begin() ; iter != scores.end() ; ++iter ) {

        std::cout << std::setw(name_and_score::MAX_NAMESIZE) << iter->name
                  << " : " << std::setw(3) << iter->score << '\n' ;
    }
}

int main() {

    // ask the user to enter the number of scores there will be
    std::size_t num_scores = 0 ;
    std::cout << "how many scores? " ;
    std::cin >> num_scores ;

    // create a vector of the required size
    std::vector<name_and_score> scores(num_scores) ;

    readData(scores) ;
    sortData(scores) ;
    displayData(scores) ;
}
I appreciate all your code as I can learn from it. However, I want to write my own program and not copy so I can learn and avoid plagiarism. I got the vector sized now according to user input. Also, In my readData function I went with an iterator style as my instructor is using to stay consistent with his teaching. Now, I am trying to figure out how to fill the c-string with names and the vector with scores. I played around with the style in your readData function but for some reason it wasn't working with my code.
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
#include <iostream>
#include <vector>
using namespace std;

const int MAX_NAMESIZE = 24;

struct Highscore {
	char name[MAX_NAMESIZE];
};

void getArraySize(size_t& numberOfScores);
void readData(vector<Highscore>& scores, size_t numberOfScores);

int main()
{
	size_t numberOfScores = 0;

	getArraySize(numberOfScores);
	vector<Highscore> scores(numberOfScores);
	readData(scores, numberOfScores);

	system("pause");
}





void getArraySize(size_t& numberOfScores)
{
	cout << "How many scores will you enter?: ";
	cin >> numberOfScores;
}






void readData(vector<Highscore>& scores, size_t numberOfScores)
{
	for (vector<Highscore>::const_iterator i = scores.begin(); i != scores.end(); i++)
	{
		cout << "Enter the name for score #" << (scores.size() - numberOfScores) + 1 << endl;
	
		
	}
}



This is what I have going for my readData function and I get errors under the extraction operator in the cin statements before i, that read:

Severity Code Description Project File Line Suppression State
Error (active) E0349 no operator ">>" matches these operands Project27

1
2
3
4
5
6
7
8
9
10
11
12
13
void readData(vector<Highscore>& scores, size_t numberOfScores)
{
	for (vector<Highscore>::const_iterator i = scores.begin(); i != scores.end(); i++)
	{
		cout << "Enter the name for score #" << (scores.size() - numberOfScores) + 1;
		cin >> setw(sizeof(i->name)) >> i->name;
		cin.ignore(1000, '\n');

		cout << "Enter the score for score #" << (scores.size() - numberOfScores) + 1;
		cin >> i->score;
	
	}
}


I got rid of the const in the for loop and that seemed to have made it work.
I got rid of the const in the for loop

A const_iterator means you can't alter the element the iterator is pointing to.
That makes sense now. Thank you.
Topic archived. No new replies allowed.