Pancake Glutton(4 star) question

This is semi spoiler code if you haven't done the work yet so don't read if thats the case.

I'm looking for some help , I have what I believe to be valid code but obviously I'm fibbing something.

my problem is when I run the program it accepts 10 entries, then sorts the code but outputs the wrong person number's, i.e...

"Person -21831293 ate 12 pancakes"
"Person -183823 ate 9 pancakes.."

the number of pancakes eaten is correct but the person's identity number is wrong...

here is 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
//Pancake Glutton
//Exercise 4 - 4 stars
#include <iostream>
#include <algorithm>

//struct to hold persons
struct Person {
    int personNum;
    int pancakesEaten;
};

//sort criteria
bool personSorter(const Person &, const Person &);


using namespace std;

int main()
{
    Person person[10];
    int i;
    for(i = 0; i<10; ++i)
    {
        cout << "Enter pancakes eaten by person " << i+1 << ": ";
        cin >> person[i].pancakesEaten;
        person[i].personNum == i;
    }
    stable_sort(person, person+10, personSorter);

    for(int i = 0; i <10; ++i)
        cout << "Person " << person[i].personNum << " ate " << person[i].pancakesEaten << " pancakes " << endl;
    return 0;
}

bool personSorter(const Person & lhs, const Person & rhs)
{
    if(lhs.pancakesEaten < rhs.pancakesEaten)
        return false;
    return true;
}


Thank you for any help you can offer... i'd appreciate it if you didn't supply me with the proper code but a nudge in the right direction would be great.
When you try to assign i to personNum, you instead compare i to it with ==, leaving personNum uninitialized. The rest should be fine.
Last edited on
(: thank you haha
Topic archived. No new replies allowed.