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:
// ovelse.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
int main()
{
usingnamespace std;
constint 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;
}
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.).
// ovelse.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
int main()
{
usingnamespace std;
int MaxPancakes = 0;
int PersonName = 0;
constint 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;
}
#include <iostream>
#include <vector>
#include <conio.h>
#include <string>
usingnamespace 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...