# C++ EXERCISE HELP (pancake challenge)

## Recommended Posts

OK so here is my attempted code as you can see i am trying to somehow implement a way that i can join up the person who ate the most pancakes to their value of pancakes eaten, i have been bashing my face into the monitor for a few hours now and have finally conceded that i need help

please could someone review my code and drop a few hints on how to make this work,

[CODE]

Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.
*/
#include "iostream"
#include <stdio.h>

using namespace std;

int person[10]= { 0,0,0,0,0,0,0,0,0,0};

// i have written out this structure with the intention of being able to print out the name of person an cakes eaten as one item
// i haven't implemented it anywhere within the code because i dont have a clue how too make it work the way that i want.

//structure to hold person data and cakes eaten
typedef struct personalDetails
{
char name[10];
int index;

}personINFO;

//SORTING OUT
void findHighest()
{
int highest = -1;
int tempA = -1;
int tempB = -1;

for(int index = 0; index < 10; index++)
{
tempA = person[index];
tempB = person[index + 1];

if(tempA > tempB)
{
highest = tempA;
}

}
cout << highest << " was the most pancakes eaten\n"; // i need to some how edit this line to print out who ate the most
}

int main (int args, char* argv[]){

//user input and instruction screen
cout << "--------------------------------PANCAKE-O-MATIC--------------------------------\n\n";
cout << "-----------please enter the number of pancakes eaten by each person------------\n\n";

for(int index = 0;index < 10; index++)
{
cout << "how many did person" << " " << index + 1 << " eat? "; // i know i need to change something here so that the number of pancakes eaten
cin >> person[index],"\n";                                                        // is attached to the person eating them i.e person 1 ate 2, person 2 ate 4 etc
}
//sort function
findHighest();

system("PAUSE");
}/*Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)

[\CODE]

##### Share on other sites
BloodyEpi    1152

Please use the codetags and indention.

If you just want to figure who ate most, you can loop through your array and hold the index of the person who ate most, for every new person you check against that index if she ate more or less, if she ate more, save that index instead, if not continue.

Your struct looks very c like, try:

struct PancakeEater
{
std::string name;
int count;
};

std::vector<PancakeEater> people;  //or std::array


##### Share on other sites
PwFClockWise    512

for(int index = 0; index < 10; index++)
{
tempA = person[index];
tempB = person[index + 1];

if(tempA > tempB)
{
highest = tempA;
}

}
cout << highest << " was the most pancakes eaten\n"; // i need to some how edit this line to print out who ate the most
}

So this part is troublesome. When the for-loop hits the final index, i.e 9, your tempB variable will attempt to gain access to person[10] which would mean that you will try to access the 11th(arrays goes from index 0-9!) index of the array. Since you are looping through 10 iterations I'm assuming that person[] is 10 in size, which would mean that the 10th index is out of bounds thus crashing your application.

I'm assuming that person[] is an int array, otherwise highest would not be able to be given tempA value. First of all I would make the person array an array of playerInfo structs, but rename it a bit like the post before me suggested:

struct PancakeEater
{
std::string name;
int nrOfPancakes;
};

PancakeEater persons[10];

// fill PancakeEater with the correct data.

int highestIndex = 0; // Now this will be the index on which the person with most pancakes eaten would be at.
int mostPancakes = 0; // This will store the amount of pancakes eaten, only used for comparison reasons.

for(int index=0; index<10; index++)
{
if(persons[index].nrofPancakes> mostPancakes)
{
highestIndex = index; //This will store on what index the pancake eater with most pancakes is located in the persons array.
mostPancakes = persons[index].nrOfPancakes; // store the new highest amount of pancakes eaten.
}

}

cout << persons[highestIndex].nrOfPancakes<< " was the most pancakes eaten\n and was done by " << persons[highestIndex].name;

Note that it's very late for me, and that this code won't work completely copied. This is also not the best naming convention or the most good looking way to do it, but it doesn't seem like that is your major concern at the moment. I gave you the essential stuff here, hopefully this will help you complete the task. Good luck!

Edited by PwFClockWise

##### Share on other sites

u sir are my new best friend, thank you

##### Share on other sites
EarthBanana    1794
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;

struct Person
{
string name;
unsigned int pancakesAte;
};

int main()
{
const int NUM_PEOPLE = 10;   // its always nice to store values that may appear more than once in variables - you can change this to any number of people now
int holdConsoleOpen;  // just a dummy variable to hold the console open at the end of the program by using cin >> holdConsoleOpen
vector<Person> people; // create a vector of the type Person. A vector is very similiar to an array only it allows you to do a lot of things in easier ways

// user input and instruction screen (as you had it)
cout << "--------------------------------PANCAKE-O-MATIC--------------------------------\n\n";
cout << "-----------please enter the number of pancakes eaten by each person------------\n\n";

unsigned int mostPancakesIndex = 0;  // keep track of the index to the vector<Person> which will refer to the person who ate the most pancakes
for(int index = 0; index < NUM_PEOPLE; index++)  // notice NUM_PEOPLE instead of 10.. if you change NUM_PEOPLE now it will change here
{
std::stringstream ss;  // This is a string stream. It works the same way cout does but it allows you to get a string version of it at any time
Person person;         // Create a person of the struct type Person (will have name and pancakesAte attributes)

ss << "Person " << index;  // Use the string stream to create a name for the person - will be "Person [current index + 1]"
// ie if index = 0 then the persons name will be "Person 1"
person.name = ss.str();    // now use the str() function of the string stream to assign the string value to person.name

cout << "how many did person " << person.name << " eat? "; // now we can use person.name
cin >> person.pancakesAte;  // and assign the input to person.pancakesAte

people.push_back(person);  // add this person to the back of the vector

if ( person.pancakesAte > people[mostPancakesIndex].pancakesAte ) // If the current person ate more pancakes than the person associated with the saved index - overwrite the saved
mostPancakesIndex = index;									  // index with the current person's index
}

std::vector<Person>::iterator iter = people.begin(); // Use a vector<Person>::iterator to go through the contents of the people vector
while (iter != people.end()) // people.end() is a special value that tells us that there are no more items in the vector to go through
{
cout << iter->name << " ate " << iter->pancakesAte << " pancakes." << endl; // Say how many pancakes the person ate
++iter; // increment the iterator
}
cout << people[mostPancakesIndex].name << " ate the most pancakes - he/she ate " << people[mostPancakesIndex].pancakesAte << " pancakes." << endl; // use the saved index to get the person that ate the most pancakes

cin >> holdConsoleOpen;
}


This is code that will do what you want with comments explaining everything..

Edited by EarthBanana

##### Share on other sites
EarthBanana    1794

Woops someone already replied while i was writing this reply - their code works fine too!!!

Edited by EarthBanana

##### Share on other sites

cheers for the input fellas i really appreciate all the help as well as the different ways i can approach the same problem, thanks for your time and input

##### Share on other sites
EarthBanana    1794

why did someone down vote both of my replies? they were at 1 each - did I do something wrong?

##### Share on other sites
Cornstalks    7030

why did someone down vote both of my replies? they were at 1 each - did I do something wrong?

It wasn't me, and I'm not sure, but it's possible they looked down on you giving a complete solution to the exercise rather than helping the OP debug the code they had written. While I personally wouldn't downvote your posts, I can't speak for others. And again, this is wild speculation on my part.

@OP: I know you're just learning C++, which means you've already got a lot to learn, but I'd highly recommend taking a few hours and just learning the basics of your debugger and how to step through a program line by line, looking at what it's doing and computing to help you know not just where your problem is, but also see exactly why the problem is happening. Debuggers are invaluable in software development, and I wish more programming tutorials would teach programming by having people run their code through the debugger (so they also learn how their debugger works).

##### Share on other sites
PwFClockWise    512

Just as Cornstalks says, learning to use the debugger is a real time saver. When I started programming in college our teacher skipped the debugger and I didn't even know it existed until I started a project with a few classmates that had a different teacher...I was using cout to check what values certain variables contained. I'll give you a heads up though, it is kind of confusing and hard to learn how to use the debugger but you will get a lot better with it quickly.

I don't know which program you are using to code, but visual studio is widely used when it comes to C++ so I will just link a tutorial on the debugger for visual studio: http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

Hope this helps you grow in C++, good luck!

##### Share on other sites

thanks for the help people, i too am baffled as to why this was down voted,

just to mention im using vs 2010 and am teaching myself c++ through various books and websites as well as a  home study course, i think i understand the basics of debugging, using breakpoints etc but i didn't think that was where my problem came from, my problem was that my understanding of how to use various loops to my advantage was limited so the above section of code was a major help, all the extra comments and explanations were a massive help in understanding various aspects.

i just want to say that this is not the complete exercise i still have a few other problems to solve and intentionally did not post them so i still had something to do

and  due to the quality of help from the above code i am also researching string stream and vectors so i can apply them to my next piece of code as these seem like hugely valuable tools im also a little upset that someone would downvote something that was a huge help and not provide a reason for it.  dissecting the above code has helped me immensely in my studies and understanding