Simplify program with arrays or switch?

Hi, I've made a program from an exercise I found here http://www.cplusplus.com/forum/articles/12974/
It's the pancake glutton one where you input how many pancakes 5 people eat and then output who ate the most. My code looks like this:

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
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int x;
	int y;
	int z;
	int u;
	int v;

	cout << "Enter the number of pancakes each person ate\n" << "1 ate: ";
	cin >> x;

	cout << "2 ate: ";
	cin >> y;

	cout << "3 ate: ";
	cin >> z;

	cout << "4 ate: ";
	cin >> u;

	cout << "5 ate: ";
	cin >> v;

	if (x>y&&x>z&&x>u&&x>v)
		cout << "1 ate the most pancakes eating " << x;
		if (y>x&&y>z&&y>u&&y>v)
			cout << "2 ate the most pancakes eating " << y;
			if (z>x&&z>y&&z>u&&z>v)
				cout << "3 ate the most pancakes eating " << z;
				if (u>x&&u>y&&u>z&&u>v)
					cout << "4 ate the most pancakes eating " << u;
					else
						cout << "5 ate the most pancakes eating " << v;


	char f;
	cin >> f;
	return 0;
}


And I have been reading about arrays and switch and watching tutorials but I can't see how it can make it much simpler. Are there any ways to use switch here since the variables are unknown and if I use arrays (say x is instead array[1]), then I have to change x into array[1] everywhere right, which will just create more text. So can anyone show me how I can use any of those two (or other basics) to make this program simpler?
An array would work but ALL the variables would be the same array, just a different position in the array for each person. Here is my version of your 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
35
36
int main()
{
	int x[5]; //the array has 5 slots, 0 to 4

	cout << "Enter the number of pancakes each person ate\n" << "1 ate: ";
	cin >> x[0];

	cout << "2 ate: ";
	cin >> x[1];

	cout << "3 ate: ";
	cin >> x[2];

	cout << "4 ate: ";
	cin >> x[3];

	cout << "5 ate: ";
	cin >> x[4];

	int MostAte = 0;
	unsigned int Person;
	for(unsigned char i = 0; i < 5; i++)
	{
		if(x[i] >= MostAte) //if this person ate more than the current victor, set them as the new victor
		{
			MostAte = x[i];
			Person = i + 1;
		}
	}

	cout << "\n\nPerson " << Person << " ate the most pancakes. They ate " << MostAte << " of them.";

	char f;
	cin >> f;
	return 0;
}
Last edited on
Aha, I think I understand. That definitely makes it easier yes, thanks!
General rule of thumb: if you find your self copy/pasting code and making slight modifications, you're likely doing something wrong.

L B's post is a big step in the right direction, but even it can be improved.

Not so good:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	cout << "Enter the number of pancakes each person ate\n" << "1 ate: ";
	cin >> x[0];

	cout << "2 ate: ";
	cin >> x[1];

	cout << "3 ate: ";
	cin >> x[2];

	cout << "4 ate: ";
	cin >> x[3];

	cout << "5 ate: ";
	cin >> x[4];


Much better:
1
2
3
4
5
6
cout << "Enter the number of pancakes each person ate\n";
for(int i = 0; i < 5; ++i)
{
    cout << (i+1) << "ate: ";
    cin >> x[i];
}



Or even better, you can put this all in the same loop so you don't even need that x array at all.
Last edited on
Yea, that makes sense, thanks!
Topic archived. No new replies allowed.