Question about pancakes.

So I was doing one of the beginner exercises from this website, which is called Pancake Glutton or something. I've done the most, I just can't figure out how to find the person who the ate the most. I'm only able to find out what the most eaten amount is. This 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

// ovelse.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <algorithm>

int main()
{
	using namespace std;

	const int AmountOfPersons = 10;
	int Persons[AmountOfPersons];

	for (int Person = 0; Person < AmountOfPersons; Person++)
	{
		cout << "Please enter how many pancakes Person " << Person << "	 had: ";
		int NumberOfPancakes;
		cin >> NumberOfPancakes;

		Persons[Person] = NumberOfPancakes;
	}

	int MaxPancakes = 0;
	for (int iii = 0; iii < AmountOfPersons; iii++)
	{
		if (Persons[iii] > MaxPancakes)
		MaxPancakes = Persons[iii];

	}

	cout << MaxPancakes << " was the most eaten amount by" << ;

	cin.clear();
	cin.ignore(255, '\n');	
	cin.get();

	return 0;
}
Put the value of iii into a variable before exiting the for loop at the same time as you get the value of MaxPancakes.
I still have no idea how to print the name of the person. I realize I get the number of the array with iii. With Persons[iii] i just get the value of that person (0, 1, etc.).


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

// ovelse.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <algorithm>

int main()
{
	using namespace std;

	int MaxPancakes = 0;
	int PersonName = 0;
	const int AmountOfPersons = 10;
	int Persons[AmountOfPersons];

	for (int Person = 0; Person < AmountOfPersons; Person++)
	{
		cout << "Please enter how many pancakes Person " << Person << "	 had: ";
		int NumberOfPancakes;
		cin >> NumberOfPancakes;

		Persons[Person] = NumberOfPancakes;
	}

	
	for (int iii = 0; iii < AmountOfPersons; iii++)
	{
		if (Persons[iii] > MaxPancakes)
		{
		MaxPancakes = Persons[iii];
		int PersonName = iii;
		}
	}

	cout << MaxPancakes << " was the most eaten amount by " << Persons[iii];

	cin.clear();
	cin.ignore(255, '\n');	
	cin.get();

	return 0;
}
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
#include <iostream>
#include <vector> 
#include <conio.h>
#include <string>
using namespace std; 

int main() {
	vector<string> names; 
	vector<int> lolcakes; 
	string name; 
	int number_pancakes = 0, max_reference = 0; 

	cout << "Please enter the names of the ten people that plan to eat pancakes." << endl; 

	for (int i = 0; i < 10; i++) {
		cout << (i + 1) << ". Name: "; 
		cin >> name; 

		names.push_back(name); //will add the name just inputted to the vector list
	}

	//now you have all ten names
	//process how many pancakes they each ate

	for (int i = 0; i < 10; i++) {
	//note how this is asked in the order of the vector, important for later...
		cout << "How many pancakes did person " << names[i] << " eat?";  
		cin >> number_pancakes; 

		lolcakes.push_back(number_pancakes); //add to the vector list
	}

	//now see who ate the most

	for (int i = 0; i < 10; i++) {

		//while iterating through the vector "lolcakes", while indexing them all, find the highest value
		if (lolcakes[i] > max_reference)
			max_reference = i; //so the value of i will correspond to the index which had the
		//highest value, therefore this will be in linear proportion to the other vector and therefore
		//indexing the other vector with i will correspond to the name

	}


	cout << "The most pancakes were eaten by " << names[max_reference] << " lol." << endl; 

	cout << "\nPress any key to exit the program....";
	_getch(); 
	return 0; 
}


Just tried and tested it and it works, let me know if you have any questions...
Last edited on
Topic archived. No new replies allowed.