1) 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.
My solution uses an array, but the program only displays the person who ate the most sentence if I put a break in the end of the if statement. Without the break the program just asks how much each person ate then exits. I'm just trying to understand exactly why the break needs to be there, or if there is a way to do it without the break.
Here is my code:
//Pancakes!
#include <iostream>
#include <limits>
using namespace std;
int main()
{
//Build array of people and set a value for most eaten
int people[9] = {};
int most = -1;
for (int n=1; n<=10; n++)
{
//Sets the number of pancakes eaten to a person value in the array
cout << "How many pancakes did person " << n << " eat? ";
cin >> people[n-1];
//Checks if the value entered above is the highest value
if(people[n-1] > most)
{
most = people[n-1];
}
}
//Line entered for formatting
cout << endl;
//Lists the person and how many pancakes they ate
for (int x=0; x<10; x++)
{
if(people[x] == most)
{
cout << "Person " << (x+1) << " ate " << most << " pancake(s), the most!" << endl;
break;
}
}
//Pause after program
cout << endl;
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
Also feel free to review my code and give me tips for making it more concise because im still a newbie =] thanks.
For the above question, I would solve it this way to preview the person who ate the most pancakes:
Code:
// assuming the first one is the highest one
most = 0;
//now checking for the higest one
for (int x = 0; x < 10; x++) {
if (people[x] > =people[most]) {
most = x;
}
}
cout << people[most]; //this shows the highest pancakes.
This doesn't use break at all and gives the required output.