Test Various Different Variables?

So I have this 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
  int main() {
	int Person1;
	int Person2;
	int Person3;
	int Person4;
	int Person5;
	int Person6;
	int Person7;
	int Person8;
	int Person9;
	int Person10;
	
	std::cout << "How many pancakes has each Person had?\n\n";
	std::cout << "Person 1: ";
	std::cin >> Person1;
	std::cout << "Person 2: ";
	std::cin >> Person2;
	std::cout << "Person 3: ";
	std::cin >> Person3;
	std::cout << "Person 4: ";
	std::cin >> Person4;
	std::cout << "Person 5: ";
	std::cin >> Person5;
	std::cout << "Person 6: ";
	std::cin >> Person6;
	std::cout << "Person 7: ";
	std::cin >> Person7;
	std::cout << "Person 8: ";
	std::cin >> Person8;
	std::cout << "Person 9: ";
	std::cin >> Person9;
	std::cout << "Person 10: ";
	std::cin >> Person10;
}


how do I make it so it will std::cout the Person who ate the least amount of pancakes?
Do you know how to use arrays (or std::vector)?
Please don't remove the #includes at the top of your code, they are necessary to make it compile for us without having to modify your code. That is, it makes it easier for us to help you.

There's a saying that if you have to repeat yourself at least three times, you should factor out the behavior accordingly.
I'm going to ignore that for now and show you an answer that "works" with your existing framework.

This uses variadic template parameters.
https://www.geeksforgeeks.org/variadic-function-templates-c/
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
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <algorithm>

template<typename T>
const T& min(const T& obj1)
{
    return obj1;
}

template<typename T, typename... Types>
const T& min(const T& person1, const T& person2, Types... people)
{
    const T& min_person = std::min(person1, person2);
    return min(min_person, people...);
}

int main() {
	int Person1;
	int Person2;
	int Person3;
	int Person4;
	int Person5;
	int Person6;
	int Person7;
	int Person8;
	int Person9;
	int Person10;
	
	std::cout << "How many pancakes has each Person had?\n\n";
	std::cout << "Person 1: ";
	std::cin >> Person1;
	std::cout << "Person 2: ";
	std::cin >> Person2;
	std::cout << "Person 3: ";
	std::cin >> Person3;
	std::cout << "Person 4: ";
	std::cin >> Person4;
	std::cout << "Person 5: ";
	std::cin >> Person5;
	std::cout << "Person 6: ";
	std::cin >> Person6;
	std::cout << "Person 7: ";
	std::cin >> Person7;
	std::cout << "Person 8: ";
	std::cin >> Person8;
	std::cout << "Person 9: ";
	std::cin >> Person9;
	std::cout << "Person 10: ";
	std::cin >> Person10;
	
	const int& PersonMin = min(
	    Person1, Person2, Person3,
	    Person4, Person5, Person6,
	    Person7, Person8, Person9,
	    Person10
	);
	 
	std::cout << "min = " << PersonMin << '\n';
	    
}


But if you want a better solution, follow what Peter87 said and use arrays or vectors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <algorithm>

int main() {

	std::vector<int> People(10);
	
	for (size_t i = 0; i < People.size(); i++)
	{
	    std::cout << "Enter data: ";
	    std::cin >> People[i];
	}
	
	std::cout << *std::min_element(People.begin(), People.end());
}
Last edited on
Please forgive the late addition to the solutions already provided, but I'd already started playing around with the question before I'd seen the other posts.

Also, I kinda understood that the original poster wanted not just the minimum number of pancakes that was eaten, but the person who ate them. Thus, the code below attempts to do that, though no doubt there are probably better and more efficient or correct, etc, ways of doing it ... but it seems to work okay.

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

int main() {
    const int peeps=5;          // the number of people eating pancakes
    int people[peeps] = {};

    for (size_t i = 0; i < peeps; i++)
    {
    std::cout << "Pancakes eaten by Person-" << i+1 <<": ";
    std::cin >> people[i];
    }

    int mincakes= *std::min_element(people, people + peeps);
	
    std::cout << "\nJust " << mincakes << " pancake(s) devoured by ";

    int myflag=0;		
    for (int x=0; x<peeps; x++)
    {
        if (people[x]==mincakes)
        {
        if (myflag) std::cout << " and ";
        std::cout << "Person-" << x+1;
        myflag=1;
        }
    }

return 0;
}


Example Output:
Pancakes eaten by Person-1: 2
Pancakes eaten by Person-2: 1
Pancakes eaten by Person-3: 1
Pancakes eaten by Person-4: 4
Pancakes eaten by Person-5: 5

Just 1 pancake(s) devoured by Person-2 and Person-3

Last edited on
Topic archived. No new replies allowed.