Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Sorting vectors

$
0
0
Alright, new question time. Program needs to accept in names and scores from the user, store them in an array, and then print them out in descending order (for the scores) and ascending alphabetical order (names). I've got the program to the point where the information is accepted in, stored. The printing of the ascending names works fine. If I try and use the same procedure to sort the numbers I get an invalid iterator range. If I comment out the code pertaining to the scores, everything works fine. This is what I have. I don't know why I'm getting the iterator error when the same procedure is being used for the other sorting.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main()
{
	vector<string> name(0);
	vector<int> score(0);
	int myScore;
	string myName;

	do
	{
		cout << "Enter the players names: " << endl;
		cin >> myName;
		name.push_back(myName);
	}
	while(name.size() != 10);
	
	cout << endl;
	cout << "Now on to the scores: " << endl;

	do
	{
	cout << "Enter in a score:" << endl;
	cin >> myScore;
	score.push_back(myScore);
	
	}
	while(score.size() != 10);

	cout << "Now to show the results: " << endl;
	cout << " ***** ***** ***** ***** " << endl;
	cout << endl;
	cout << "scores in descending (highest to lowest) order " << endl;
	sort(score.end(), score.begin());
	sort(name.begin(), name.end());

	for (int i = 0; i<score.size(); ++i)
	{
		
		cout << score[i] << endl;
	}
	cout << "Names in alphabetical order " << endl;
	for (int i = 0; i<name.size(); ++i)
	{
		
		cout << name[i] << endl;
	}



	

system("PAUSE");
return 0;
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>