Pancake Glutton Beginner Challenge Question

Hi everyone, I have been teaching myself c++ for about two weeks now, I love learning new stuff about it and I am finding the best way to make concepts stick for me, is to write example programs. I found a thread on here about beginner challenges, but I am stuck on one part of the challenge.

I have to basically, use an array to store how many pancakes each of 10 people ate. After that I need it to read the results, then I need it to tell me which person ate the most pancakes.

So far I have the program able to store the 10 peoples pancakes, read the results, and tell me the greatest amount of pancakes eaten. But I cannot figure out how to have to the program tell me which person ate the most pancakes.

My code is below (please correct any bad habits or anything as well, as I want to nip any of those in the bud, before I start learning more advanced topics. Thanks!

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
  #include <iostream>

using namespace std;

int main()
{
	int people[10];
	int temp = 0;


	for (int pancakes = 0; pancakes < 10; pancakes++)
	{
		cout << "How many pancakes did person " << pancakes+1 << " eat? ";
		cin >> people[pancakes];
		cin.ignore();
	}

	cout << "\n";

	for (int pancakes = 0; pancakes < 10; pancakes++)
	{
		cout << "Person " << pancakes+1 << " ate " << people[pancakes] << " pancakes" << endl;
	}

	for (int i = 0; i < 10; i++)
	{
		if (people[i]>temp)
			temp = people[i];
	}

	cout << "The greatest amount of pancakes eaten was " << temp;
	
	cin.get();

	return 0;
}
In the for loop determining the greatest amount of pancakes add another variable to record the index + 1.

1
2
3
4
5
6
7
8
9
int person;
for(int i=0;i<10;i++)
{
if(people[i]>temp)
{
temp=people[i];
person=i+1;
}
}
Last edited on
Hi Cody thanks for the answer! I tried your solution, unfortunately since the loop runs ten times, person always ends up being equal to ten. But I think you may have pushed me in the right direction, by using another variable to track the people. Before I was thinking along the lines of, find the greatest number and record what Array element that number was in, but I am not sure if thats possible? Atleast without a more comprehensive understanding of c++ (which I don't have yet).
Hey guys, I figured it out, it was basically what Cody said, I guess my code earlier was kind of messy and it didn't work out as expected, but I cleaned everything up and finally got it working. Check it out below, if you guys spot any room for improvement please let me know, I want to avoid bad habits before getting to the more advanced stuff.

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
#include <iostream>

using namespace std;

int main()
{
	int array[10];

	for (int i = 0; i < 10; i++)
	{
		cout << "Enter how many pancakes person " << i + 1 << " has eaten ";
		cin >> array[i];
		cin.ignore();
	}

	cout << "\n";

	for (int i = 0; i < 10; i++)
	{
		cout << "Person " << i + 1 << " ate " << array[i] << " pancakes";
		cout << "\n";
	}
	


	int most = array[0];
	int least = array[0];
	int personm;
	int personl;

	for (int i = 0; i < 10; i++)
	{
		if (array[i] > most)
		{
			most = array[i];
			personm = i + 1;
		}
		else if (array[i] < least)
		{
			least = array[i];
			personl = i + 1;
		}
	}

	cout << "\n";
	cout << "The most amount of pancakes eaten was " << most << " by person " << personm << endl;
	cout << "The least amount of pancakes eaten was " << least << " by person " << personl << endl;
	
	cin.get();

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