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:
#include "stdafx.h"
#include <iostream>
usingnamespace 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:
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;
unsignedint Person;
for(unsignedchar 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;
}