Pancakes program

Hello, my name is Justin and i have just started coding two weeks ago. I was writing a program, that should ask 10 people how many pancakes have they ate for breakfast and then print them in order like:
Person 7 ate 10 pancakes
Person 3 ate 8 pancakes
Person 9 ate 7 pancakes
and ect...

I know how to find maximum pancakes amount and minimum pancakes amount. Can you help me finish writing this program?

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

using namespace std;

int main()
{

    int amount[11],number,MaxNR,MaxP=0;

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

        cout<<"How many pancakes does person nr "<<number<<" ate?"<<endl;
        cin>>amount[number];
        if(amount[number]>MaxP)
        {
            MaxP=amount[number];
            MaxNR=number;
        }

    }

    cout<<"Person nr "<<MaxNR<<" ate "<<MaxP<<" pancakes"<<endl;
    return 0;
}
Nour to find a way to associate the number of the person and the amount of pancakes eaten so you can print them all.
It's better to use struct for this problem so you can store the number of the person and the amount of pancakes eaten.
Also don't get into the bad habit of using old c-style arrays. Better use std::array or std::vector.
Have a look at this demo.
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
#include <iostream>
#include <array>

using namespace std;

const int MAX_EATERS = 3; // choose any number you want

struct Eater
{
  int number;
  int amount;
};

int main()
{
  array<Eater, MAX_EATERS> eaters;

  for (int i = 0; i < MAX_EATERS; i++)
  {
    cout << "How many pancakes did person #" << i + 1 << " ate? ";
    int num_eaten;
    cin >> num_eaten;
    eaters[i].number = i + 1;
    eaters[i].amount = num_eaten;
  }
  // print all 
  for (Eater e : eaters)
  {
    cout << "Person #" << e.number << " ate " << e.amount << " pancakes" << "\n";
  }
  return 0;
}
Hello JustinPlusPlus,

Welcome to the forum.

Since you are just getting started you should learn some good habits. Here is your program with some changes to make it easier to read. I added some blank lines to make it easier to read and not the comments in the program:

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

//using namespace std;  // <--- Best not to use. It WILL get you in trouble some day.

int main()
{

	int amount[11]{}, number{}, MaxNR{}, MaxP = 0;  // <--- Always initial your vraiables. The last on is OK.

	for (number = 0; number < 10; number++)  // <--- Arrays start at zero not one. Changed.
	{
		std::cout << "How many pancakes does person nr " << number << " ate? ";  // <--- Best not to use the "endl" here. Notice the space after the ?.

		cin >> amount[number];

		if (amount[number] > MaxP)
		{
			MaxP = amount[number];
			MaxNR = number;
		}
// <--- This blank line not needed. Needs to be removed.
	}

	std::cout << "Person nr " << MaxNR << " ate " << MaxP << " pancakes" << << std::endl;

	return 0;

}


I added a space around the "<" in the for loop and if statements.

If you learn good habits now it will make your life easier later.

The line using namespace std; has always been a big problem with new people. It does allow you to do less typing, but does not always work the way you want. As an example try this and see what errors you get. When you do not understand it let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int count{};  // <--- Even as a global variable it is best to initialize and not rely on the compiler to do it for you.

int main()
{
	std::cout << count << std::endl;

	count++;

	std::cout << count << std::endl;
}  // End main 


When you started to work on this program what information are you using to go by? This is not the first program here of this type and I have seen it done several different ways. I believe that most of these programs deal with a name and amount eaten. Thomas1965 has a very good suggestion, but that would depend on what you have learned so far and what you want to learn.

Doing a search here on "pancakes" or "pancake glutton" should produce previous posts on this program to look at.

Your best approach here is to work in small parts, get them working and move on to the next part instead of trying to do the whole program at one time.

When you and I have a better idea of what you need to do with this program it will be easier to move forward.

Hope that helps,

Andy
Topic archived. No new replies allowed.