How to output which user inputted each value?

I'm working through the beginner exercises and have got to 'Pancake Glutton'.

"Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes"

I've managed to output the most and least number of pancakes eaten, but I'm stuck on the last bit!
I can output the number of pancakes eaten in descending order i.e. 6,5,4,3,2 etc. but I'm struggling to work out how to assign each number of pancakes the user who ate them?
I tried using the same technique that I used with the other two for loops, but I know it isn't correct, as this method doesn't assign the user to their number of pancakes eaten.

I feel like I'm missing something pretty simple here! Thanks for any help!

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>

using namespace std;

int main()
{
    cout << "how many pancakes did you eat for breakfast?" << endl;

    int person1, person2, person3, person4, person5;
    cout << "Person 1: ";
    cin >> person1;

    cout << "Person 2: ";
    cin >> person2;

    cout << "Person 3: ";
    cin >> person3;

    cout << "Person 4: ";
    cin >> person4;

    cout << "Person 5: ";
    cin >> person5;

    int array[5] = {person1, person2, person3, person4, person5};
    int temp = 0;
    int res = -1;

    for (int i = 0; i < 5; i++)
    {
        if (array[i] > temp)
        {
        temp = array[i];
        res = i;
        }
    }
    cout << "The most pancakes eaten was " << temp << " by Person " << (res+1) << endl;

    int smallest = array[0];
    int res2 = 0;

    for (int i = 1; i < 5; i++)
    {
        if (array[i] < smallest)
        {
        smallest = array[i];
        res2 = i;
        }
    }
    cout << "The least pancakes eaten was " << smallest << " by Person " << (res2+1) << endl;

    int temp3 = 0;
    int res3 = -1;

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            if (array[i] > array[j])
            {
                temp3 = array[i];
                array[i] = array[j];
                array[j] = temp3;
                res3 = i;
            }
        }
    }

    for (int i = 0; i < 5; i++)
    {
        cout << "Person " << (res3+1) << " ate " << array[i] << " pancakes" << endl;
    }
}
Last edited on
Create an array

int personId[5] = {1,2,3,4,5};

In the last if statement, when you swap the number of pancakes, swap the personId as well.
try 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
50
#include <iostream>
#include <string>
#include <array>

using namespace std;

class People
{
	unsigned int m_nPancakesEaten;
public:
	People(void) : m_nPancakesEaten(0) {}
	unsigned int getNumPancakesEaten(void) const
	{
		return m_nPancakesEaten;
	}
	void setNumPancakesEaten(unsigned int n)
	{
		m_nPancakesEaten = n;
	}
	void operator ++ (void)
	{
		m_nPancakesEaten ++;
	}
};

std::array<People , 10 > peoples;

int main(int argc , char * argv[])
{

	int i = 0;

	do
	{
		std::cout << "Enter the number of pancakes eaten by people n." << i + 1 << std::endl;
		unsigned int temp = 0;
		std::cin >> temp; 
		peoples[i ++].setNumPancakesEaten(temp);
	} while( i < 10);

	std::sort(peoples.begin() , peoples.end() , 
		[&](const People & a , const People & b) -> bool { return a.getNumPancakesEaten() < b.getNumPancakesEaten();} );

	std::cout << "lowest :" << peoples.at(0).getNumPancakesEaten() << std::endl;
	std::cout << "highest :" << peoples.back().getNumPancakesEaten() << std::endl;

	system("pause");

	return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.