Pancake glutton program. (C++)
Requires: variables, data types, and numerical operators basic input/output logic (if statements, switch statements) loops (for, while, do-while) arrays
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) Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.
★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.
★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people. i.e. Person 4: ate 10 pancakes Person 3: ate 7 pancakes Person 8: ate 4 pancakes ... Person 5: ate 0 pancakes
Current version I have written: http://codepad.org/QHnt11CT
#include <iostream>
#include <string>
void bubbleSort(int ar开发者_高级运维r[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
int main()
{
int pancakeAmount[10];
std::string consumers[10];
for (int i = 0, j = 1; i < 10; i++, j++) {
std::cout << "Please enter an amount of pancakes eaten by consumer"\
" number " << j << "." << std::endl;
std::cin >> pancakeAmount[i];
std::cout << "Please enter the name of the person who ate that amount"\
" of pancakes." << std::endl;
getline(std::cin, consumers[i]);
}
std::cout << "The results from least amount eaten to the greatest amount"\
" eaten are as follows:" << std::endl;
bubbleSort(pancakeAmount, 10);
for (int k = 0; k < 10; k++) {
std::cout << pancakeAmount[k] << std::endl;
}
return 0;
}
That is the problem I am currently working on. As of right now, the first two objectives of this problem have been solved without any notable issues.
The third objective, however, is proving to be a bit more difficult. I am having a hard time designing/implementing a sorted list with the appropriate labels. In line 47 I am trying to obtain a name or label to give to the corresponding amount.
I am running into an issue where the console will accept the amount I want to assign, but will completely ignore the call to the getline() function and loop back into asking for another amount. When the getline() function is called before the "std::cin >> pancakeAmount[i]" is called, I can give input on the first loop, but successive loops produce the error I was encountering when the getline() function was in it's original position in the code.
Am I trying to utilize an array of strings in an improper fashion, or is the getline() function not being used properly?
So today is your lucky day, because usually I don't do homework.
Your problem is that you need to ignore the return of your cin.
So this should solve your problem but it is not tested:
struct PanCakeEater
{
int pancakeEaten;
std::string name;
};
bool PanCakeSort(PanCakeEater const& eater1, PanCakeEater const& eater2)
{
return eater1.pancakeEaten < eater2.pancakeEaten;
}
int main()
{
std::vector<PanCakeEater> eaters;
const size_t numberOfEaters = 3;
for(size_t i = 0; i < numberOfEaters; ++i)
{
PanCakeEater p;
std::cout << "Please enter an amount of pancakes eaten by consumer number " << i << "." << std::endl;
std::cin >> p.pancakeEaten;
std::cin.ignore(); // ignore the \n
std::cout << "Please enter the name of the person who ate that amount of pancakes." << std::endl;
getline(std::cin, p.name);
eaters.push_back(p);
}
std::sort(eaters.begin(), eaters.end(), PanCakeSort);
for(auto iter = eaters.begin(); iter != eaters.end(); ++iter)
{
std::cout << iter->name << ": " << iter->pancakeEaten << std::endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int ate[10],eater[10],hold,temp;
cout<<"\t\tEnter How Many Pancakes Did Each Person Eat?\n\n";
for(int h=0;h<10;h++)
{
eater[h]=h;
}
for(int q=0;q<10;q++)
{
cout<<"Person "<<eater[q]+1<<" Ate: ";
cin>>ate[q];
}
cout<<"____________________________________________\n\n"<<"\t\tOrdered List Of Above.\n\n";
for(int a=0;a<10;a++)
{
for(int b=0;b<10;b++)
{
if(ate[b]<ate[b+1])
{
hold=ate[b];
ate[b]=ate[b+1];
ate[b+1]=hold;
temp=eater[b];
eater[b]=eater[b+1];
eater[b+1]=temp;
}
}
}
for(int w=0;w<10;w++)
{
cout<<"Person "<<eater[w]+1<<" Ate "<<ate[w]<<" Pancakes\n";
}
return 0;
}
Here is a simple version of it. Must be easier for some beginners to understand.
#include <iostream>
#include <string>
using namespace std;
int main() {
int y;
cout << "How many people do you want to enter= \n";
cin >> y;
string names[10];
int pancakes[10];
for (int i = 0; i <= y-1 ; i++) {
cout << "enter name= ";
cin >> names[i];
cout << "num of pancakes= ";
cin >> pancakes[i];
}
int maxEaten = pancakes[0];
int minEaten = pancakes[0];
string maxPer = names[0];
string minPer = names[0];
for (int k = 1; k < y; k++) {
if (pancakes[0] < pancakes[k]) {
maxEaten = pancakes[k];
maxPer = names[k];
}
if (pancakes[0] > pancakes[k]) {
minEaten = pancakes[k];
minPer = names[k];
}
}
cout << maxPer << " ate " << maxEaten << " which is the maximum." << endl;
cout << minPer << " ate " << minEaten << " which is the minimum." << endl;
system("pause");
}
精彩评论