Stumped (loops, if statements, arrays)

So I am slowly but surely getting better at writing code, but am still not quite able to read the description of what the program is asking for, and automatically know how I am going to write it. And I've been staring at this one for a good while trying to figure it out but figured it is something very simple, and hopefully someone can at least clue me in on what to do next.

The directions are listed in italics below:



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.

Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.


I got the first part finally, but it took me much longer than I thought it would. I'm just wondering if anyone can clue me in on what I need to do to display the minimum number of pancakes eaten by one person, and figured I'd post this while I am trying to figure it out on my own. Any help and/or advice is greatly appreciated. I'm getting the hang of this but still not quite where I can get going without hitting a road block.



#include<iostream>
using namespace std;

int main () {


int pancount; //This is the number of pancakes each person ate

int person; //This is the variable assigned to persons 1-10

int topcake = 0; //This is the value of the most pancakes eaten by one person

int bottomcake = 0; //This is the value of the least pancakes eaten by one



for (person=1; person <= 10; person++) {



cout << "Please Enter the number of pancakes eaten by Person " << person;
cin >> pancount;

if (pancount > topcake) {
topcake = pancount;

}


}

cout << "The highest number of pancakes eaten by one person is " << topcake;
cin.ignore();
cin.get();

}

I added comments to the variables above. Not sure if questions like this are what this forum is for, but posting in forums has been the way I have learned best in the past, so having at least online contact with people doing this same thing would really help me.


Last edited on
You're new here, so I'll give you a break, but from now on: indent your code and use code wrap. Please.

The easiest way to find the smallest number would be to use an array to enter the information.

You could also just set bottom cake to a ridiculously high number the first time, and say:
if(pancount < bottomcake) then set them equal to each other.

Hope this helps.
Are you familiar with arrays at all? This problem is easily solved with an array and a few other variables. Otherwise I suggest making variables for leastEaten and mostEaten, that way you can update each one during your for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(i = 0; i < 10; i++)
{
      cout << "Please Enter the number of pancakes eaten by Person " 
             << i + 1;
      cin >> pancount;

      if (pancount > topcake)
     {
              topcake = pancount;
              mostEaten = i + 1;
      }
      else if (pancount < bottomCake)
      {
             bottomCake = pancount;
             leastEaten = i +1;
      }
}

cout << "the most pancakes were eaten by person " << mostEaten 
       << "they ate " << topcake << " pancakes.";
Topic archived. No new replies allowed.