Instead of having a bunch of variables named
person1
,
person2
, etc, have a single array like:
int person[10];
Thereafter, you can say
person[0]
,
person[1]
, etc.
Also, your if..else conditions don't do what you think they do.
Since you are going to use an array now, just loop over your array to find the index (0..9) of the largest value. Once found, you can say:
cout << "Person " << (found_index + 1) << " ate the most pancakes.\n";
Hope this helps.
[edit]
Um, reading your commentary up top, the above solution (finding the single person who ate the most pancakes) is not useful to you. You should reconsider how you store your data.
How about a structure, like this:
1 2 3 4 5
|
struct person
{
int number;
int pancakes_eaten;
};
|
Then you can have an array of them:
1 2
|
person people[10];
int number_of_people = 0;
|
Now you can ask for up to ten people:
1 2 3 4 5 6 7 8 9 10
|
cout << "How many people (1-10)? ";
cin >> number_of_people;
cout << "How many pancakes eaten for person\n";
for (int n = 0; n < number_of_people; n++)
{
cout << (n+1) << "? ";
people[n] = n+1;
cin >> people[ n ].pancakes_eaten;
}
|
Next you need to
sort your people by comparing the number of pancakes eaten. So, for example, if you are given four people:
(person number, number of pancakes eaten)
(1, 3)
(2, 12)
(3, 5)
(4, 9)
After sorting them you would have:
(2, 12)
(4, 9)
(3, 5)
(1, 3)
See how they are sorted from most to least pancakes eaten.
Use the std::sort() function (in <algorithm>) to sort. You'll have to write a function that takes two people and compares them:
1 2 3 4 5
|
bool compare_people( const person& a, const person& b )
{
// greater than, since we are sorting from most to least pancakes eaten
return a.pancakes_eaten > b.pancakes_eaten;
}
|
If you must write the sort function yourself, then I recommend one of the following:
Insertion Sort
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/
Selection Sort
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/selection-sort/
All this may seem like a lot, but it really is easier to do properly than what you have above (which doesn't do what you think it does).
Hope this helps.