• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
VaugeCookie

C++ EXERCISE HELP (pancake challenge)

10 posts in this topic

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]

Edited by VaugeCookie
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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! smile.png

Edited by PwFClockWise
1

Share this post


Link to post
Share on other sites
#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
1

Share this post


Link to post
Share on other sites

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

Edited by EarthBanana
0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

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).

1

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
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 dosmile.png

 

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

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0