how could i have done this better, plz dont laugh too much at dodgy code

the idea is to enter how many pancakes everyone ate and whoever ate the most the computer tells you, it was a challenge to see if my programming is up to scratch yet...most definitely not

so it sort of works i think so long as everyone's code is 1 and the guy who ate the mosts 2 or more...i could tell i was repeating the code and that is wrong plus im sure there was a more elegant way to use the if else like i did...just could get my head round it

and i knew the answer was in creating a class that had a function that would help in just i couldnt think of a creative way to get my answers as i wanted



#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

class fatty {
public:
string name;
int pancakes;
void getcake ()
{
cout << "the fat bastard called "<< name <<" ate the most cake!!" << endl;
}

};


int main()

{

fatty fat1;
fat1.name = "bubba";
fatty fat2;
fat2.name = "cartman";
fatty fat3;
fat3.name = "elvis";
fatty fat4;
fat4.name = "santa";
fatty fat5;
fat5.name = "yo momma";
fatty fat6;
fat6.name = "kublah kahn";
fatty fat7;
fat7.name = "mr creosate";
fatty fat8;
fat8.name = "doctor lipo";


int pancakes;
int pancakes2;
int pancakes3;
int pancakes4;
int pancakes5;
int pancakes6;
int pancakes7;
int pancakes8;


cout << "how many pancakes did " << fat1.name << " have?" << endl;
cin >> pancakes;
cout << "how many pancakes did " << fat2.name << " have?" << endl;
cin >> pancakes2;
cout << "how many pancakes did " << fat3.name << " have?" << endl;
cin >> pancakes3;
cout << "how many pancakes did " << fat4.name << " have?" << endl;
cin >> pancakes4;
cout << "how many pancakes did " << fat5.name << " have?" << endl;
cin >> pancakes5;
cout << "how many pancakes did " << fat6.name << " have?" << endl;
cin >> pancakes6;
cout << "how many pancakes did " << fat7.name << " have?" << endl;
cin >> pancakes7;
cout << "how many pancakes did " << fat8.name << " have?" << endl;
cin >> pancakes8;

if (pancakes > pancakes2)
{
if (pancakes > pancakes3)
{
if (pancakes >pancakes4)
{
if (pancakes > pancakes5)
{
if (pancakes > pancakes6)
{
if (pancakes > pancakes7)
{
if (pancakes > pancakes8)
fat1.getcake ();
}

}
}
}
}
}


else if (pancakes2 > pancakes)
{
if (pancakes2 > pancakes3)
{
if (pancakes2 >pancakes4)
{
if (pancakes2 > pancakes5)
{
if (pancakes2 > pancakes6)
{
if (pancakes2 > pancakes7)
{
if (pancakes2 > pancakes8)
fat2.getcake ();
}

}
}
}
}
}



else if (pancakes3 > pancakes)
{
if (pancakes3 > pancakes2)
{
if (pancakes3 >pancakes4)
{
if (pancakes3 > pancakes5)
{
if (pancakes3 > pancakes6)
{
if (pancakes3 > pancakes7)
{
if (pancakes3 > pancakes8)
fat3.getcake ();
}

}
}
}
}
}


else if (pancakes4 > pancakes)
{
if (pancakes4 > pancakes2)
{
if (pancakes4 >pancakes3)
{
if (pancakes4 > pancakes5)
{
if (pancakes4 > pancakes6)
{
if (pancakes4 > pancakes7)
{
if (pancakes4 > pancakes8)
fat4.getcake ();
}

}
}
}
}
}

else if (pancakes5 > pancakes)
{
if (pancakes5 > pancakes2)
{
if (pancakes5 >pancakes3)
{
if (pancakes5 > pancakes5)
{
if (pancakes5 > pancakes6)
{
if (pancakes5 > pancakes7)
{
if (pancakes5 > pancakes8)
fat5.getcake ();
}

}
}
}
}
}

else if (pancakes6 > pancakes)
{
if (pancakes6 > pancakes2)
{
if (pancakes6 >pancakes3)
{
if (pancakes6 > pancakes5)
{
if (pancakes6 > pancakes4)
{
if (pancakes6 > pancakes7)
{
if (pancakes6 > pancakes8)
fat6.getcake ();
}

}
}
}
}
}

else if (pancakes7 > pancakes)
{
if (pancakes7 > pancakes2)
{
if (pancakes7 >pancakes3)
{
if (pancakes7 > pancakes5)
{
if (pancakes7 > pancakes4)
{
if (pancakes7 > pancakes6)
{
if (pancakes7 > pancakes8)
fat7.getcake ();
}

}
}
}
}
}

else if (pancakes8 > pancakes)
{
if (pancakes8 > pancakes2)
{
if (pancakes8 >pancakes3)
{
if (pancakes8 > pancakes4)
{
if (pancakes8 > pancakes5)
{
if (pancakes8 > pancakes6)
{
if (pancakes8 > pancakes7)
fat8.getcake ();
}

}
}
}
}
}

else {
cerr << "summint whent wrong" <<endl;
}
return 0;

}
actually it would just be easier to use an array:

[code
fatty fats[8];
[/code]

then loop through each value when you want to do something.

I would start there.

i feel so stupid...why didnt I think of that

AND CLASSES IN ARRAYS ARRGH
Last edited on
You over complicated this task... You can simply use an array to store each amount like pogrady said and then use a loop to determine which wan ate the most
Topic archived. No new replies allowed.