Some seasonal Pancake Glutton

Evening all,

Trying to tackle the Pancake Glutton exercise and been looking at trying to sort() an Array and failing somewhat at it.

I'm having problems with the sort() function. I understand the logic of giving it a place to start and a place to finish and then a method to sort by, but none of the 4 methods I've tried are accepted. Would someone be able to shed some light on this for me please? Thanks!

The error I'm most miffed by is: no operator "[]" matches these operands. operand types are: PanPerPerson [int]

I learnt using Python and I'm guessing there's a problem with the circumstances you can use square brackets to say which element of a Stuct/Array to look at?

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 "stdafx.h"
#include <iostream>
#include <algorithm>

struct PanPerPerson
{
	int nNumberOfPancakes;
	int nPersonNumber;
};

int getNumber()
{
	using namespace std;
	int x;
	cout << "Enter number of pancakes " << endl;
	cin >> x;
	return x;
}

void printPancakes(PanPerPerson arPancakes)
{
	using namespace std;
	for (int x = 0; x < 10; x++)
	{
		cout << "Person " << arPancakes.nPersonNumber << " ate " << arPancakes.nNumberOfPancakes << "pancakes" << endl;
	}
}

bool wayToSort(const PanPerPerson x, const PanPerPerson y)
{
	return x.nNumberOfPancakes > y.nNumberOfPancakes;
}

void sortPancakes(PanPerPerson arPancakes)
{
	using namespace std;
	sort(arPancakes.begin(), arPancakes.end(), wayToSort);
	sort(begin(arPancakes), end(arPancakes), wayToSort);
	sort(arPancakes[0], arPancakes[10], wayToSort);
	sort(arPancakes, arPancakes + 10, [](PanPerPerson a, PanPerPerson b){return a.nNumberOfPancakes > b.nNumberOfPancakes; });
}

int main(PanPerPerson arPancakes)
{
	using namespace std;
	int nLargest = 0, nSmallest = 0;
	PanPerPerson arPancakes[10];
	
	int count = 0;
	for (int x = 0; x < 10; x++)
	{
		arPancakes.nNumberOfPancakes = getNumber();
		if (count < 1)
			nSmallest = arPancakes.nNumberOfPancakes;
			count++;
	}
	
	sortPancakes(arPancakes);
	printPancakes(arPancakes);
	for (int x = 0; x < 10; x++)
	{
		if (arPancakes[x] > nLargest)
		{
			nLargest = arPancakes[x];
		}
		if (arPancakes[x] < nSmallest)
		{
			nSmallest = arPancakes[x];
		}
	}
		cout << "Largest = " << nLargest << endl;
		cout << "Smallest = " << nSmallest << endl;

}
The only problem I see is on line 39, where you are trying to sort from arPancakes[0] to arPancakes[10]...however using the array subscript in sort is incorrect, because that returns the item itself. Change it one of the other sort usages (like .begin()/.end()) and it should work fine.
Here is how I would have done it -

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

        int pancakesEaten[10];
	for (int i = 0; i < 10; i++)
	{
		cout << "How many Pancakes did " << i +1 << " eat: ";
		cin >> pancakesEaten[i];
	}

	int smallest = pancakesEaten[0];
	int largest = pancakesEaten[0];
	
	for (int i = 0; i < 10; i++)
	{
		if (pancakesEaten[i] < smallest)
		{
			smallest = pancakesEaten[i];
		}

		if (pancakesEaten[i] > largest)
		{
			largest = pancakesEaten[i];
		}
	}

	cout << endl << "Largest amount of pancakes eaten is: " << largest << endl;
	cout << "Smallest amount of pancakes eaten is: " << smallest << endl;
Thanks both for the replies.

There are errors on 37,38,39 & 40. I've tried using a couple of combinations of the begin/end but both are giving me errors.

The first I tried using the .begin() and .end() method but the arPancakes.nNumberOfPancakes.begin() gives me an 'expression must have class type' error, and the arPancakes.end() gives me a 'PanPerPerson has no member "end" '. I know that the begin and end should be the same, but for demonstration purposes I'm showing that I've tried two different ways of using .begin() and .end()

The second I tried gives me an error 'no operator "[]" matches these operands. operand types are: PanPerPerson [int]'


1
2
3
4
sort(arPancakes.nNumberOfPancakes.begin(), arPancakes.end(), wayToSort);

sort(begin(arPancakes[0]), end(arPancakes[10]), wayToSort);
Topic archived. No new replies allowed.