Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualEarthBanana

Posted 17 August 2013 - 04:11 PM

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


#1EarthBanana

Posted 17 August 2013 - 04:09 PM

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


PARTNERS