Thanks for the help folks, very much appreciated, slowly but surely i am getting the hang of this.
Right now i can input values into the vector, print them out, organise them lowest to highest, then print them out reorganised with this:
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
|
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<Windows.h>
#include<ctime>
#include <cstdlib>
using namespace std;
//*******************************************//
int main()
{
vector <int> person(11);
cout << "\n Welcome to Pancake Glutton";
cout << "\n\n\n There are 10 people,";
//cout << "Person 10 = " << person[10];
//This seems to capture the values of "person" nicely
for (int n=1; n<11; n++)
{
cout << "\n Please enter the number eaten by Person " << n << " : ";
cin >> person[n];
}
//This seems to print the values of person just right so i can see whats what
for (int n=1; n < 11; ++n)
cout << "\nPerson " << n << " ate " << person[n];
//sort the vector from smallest to largest number
sort(person.begin()+1, person.begin()+11);
//Just for the hell of it, print out the values of the sorted vector to see if it is actually ordering them
cout << "\n\nPerson 1 has value " << person[1] << "\n";
cout << "Person 2 has value " << person[2] << "\n";
cout << "Person 3 has value " << person[3] << "\n";
cout << "Person 4 has value " << person[4] << "\n";
cout << "Person 5 has value " << person[5] << "\n";
cout << "Person 6 has value " << person[6] << "\n";
cout << "Person 7 has value " << person[7] << "\n";
cout << "Person 8 has value " << person[8] << "\n";
cout << "Person 9 has value " << person[9] << "\n";
cout << "Person 10 has value " << person[10] << "\n";
//This does order them with no errors providing input is reasonable (like no minus entries)
system ("PAUSE");
return 0;
}
|
Now this is all very well but i am struggling to attach a value to the person and keep it that way so that it sticks with it. I know why its doing it but i cant come up with a different way of doing it so it retains the person number to the value.
At the moment i get this:
Person 1 = 2
Person 2 = 6
Person 3 = 4 etc
Then it orders itself and prints
Person 1 = 2
Person 2 = 4
Person 3 = 6
Instead of what it should be which is
Person 1 = 2
Person 3 = 4
Person 2 = 6 etc etc.
Like i said i just cant seem to think of a way to make 2 values that bind themselves to each other so ordering them orders one value (the amount) but drags another value (the person number) along with it.
Sorry for the horrible explanation but i cant think of another way to write it. Im thinking i have to scrap what i have started in order to get it to do what i need to because the position of vector [1] (or whatever) is always going to remain, shuffling one set of numbers will not shuffle that with it, i understand this, but cant think how to start.
edit: the code above is not meant to deal with the order of the person number being attached to the value that the vector holds, just to point out that i can organise that data from lowest to highest, i realise that i havent put in a way to make it come out right.