Vector pointed by iterator only recognizing one data submember

I'm using vectors to obtain and sort a "highscore" struct that has an integer score variable and a character array name one. I use two iterators, i and j, to sort the vector. The program recognizes j when I use it to access the score value, but says that j is undefined and undeclared in the line strcpy(max_name, j -> name).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sortData(vector<highscore> &high) 
{
	int max;
	char max_name[24];
	for (vector<highscore>::iterator i = high.begin(); i != high.end() - 1; i++) {
		max = i -> score;
		for (vector<highscore>::iterator j = high.begin() + 1; j != high.end(); j++)
			if ((j -> score) > (i -> score))
				max = j -> score;
				strcpy(max_name, j -> name);
		if (max != i -> score) {
			swap(i -> score, max);
			swap(i -> name, max_name);
		}
	}
}
1
2
3
4
for (vector<highscore>::iterator j = high.begin() + 1; j != high.end(); j++)
			if ((j -> score) > (i -> score))
				max = j -> score;
                                strcpy(max_name, j -> name);


is the same as

1
2
3
4
5
6
7
8
for (vector<highscore>::iterator j = high.begin() + 1; j != high.end(); j++)
{
    if ((j -> score) > (i -> score))
    {
	max = j -> score;
    }
}
strcpy(max_name, j -> name);


See the problem?
Last edited on
Oh, of course. Thank you.
You have other problems, and it can be simplified:

1
2
3
4
5
6
7
8
9
10
11
void sortData(vector<highscore> &high) {
    if (high.size() < 2) return;
    for (auto i = high.begin(); i != high.end() - 1; i++) {
        auto m = i;
        for (auto j = i + 1; j != high.end(); j++)
            if (j->score > m->score)
                m = j;
        if (m != i)
            swap(*i, *m);
    }
}

Topic archived. No new replies allowed.