Beginner Exercise Code Help

I'm trying to complete this exercise:


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

source: http://www.cplusplus.com/forum/articles/12974/

Here is the code

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void sortArray(int[], int);
void showArray(int[], int);


int main()
{
int numPeople;
int * pancakes;
int count;

// Gets the number of people to process
cout <<" How many people would you like to process"<<endl;
cin >> numPeople;
// Allocate array to user inputed size

pancakes = new int[numPeople];

// Gets the pancakes eaten from each person

cout << " Enter the number of pancakes eaten by each person below"<< endl;

for (count= 0; count < numPeople; count++)

cout << "Person" << (count + 1) << "ate : ";
cin >> pancakes[count];
while (pancakes [count] < 0)
{
cout << "Pancakes eaten cannot be less than 0" << endl;
cout << "Re enter";
cout << "Person " << (count + 1) << "ate : ";
cin >> pancakes[count];
}


//sort the values
sortArray(pancakes,numPeople);



// Array functions //
}


void sortArray(int array[], int size)
{
bool swap;
int temp;

do
{
swap = false;
for (int count= 0; count< (size - 1); count++)
{
if (array[count] > array[count+1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap= true;
}
}
} while (swap);
}

void showArray(int array[], int size)
{
for (int count= 0 ; count< size; count++)
cout << array[count] << " , " ;

}

I have tried to move and retype the code and every time I get a different problem. Any suggestions?
Please enclose your codes in a code bracket. :)
Your loop is messed up.
1
2
3
4
5
6
7
8
9
10
11
for (count= 0; count < numPeople; count++)
//the problem is here
cout << "Person" << (count + 1) << "ate : ";
cin >> pancakes[count];
while (pancakes [count] < 0)
{
cout << "Pancakes eaten cannot be less than 0" << endl;
cout << "Re enter";
cout << "Person " << (count + 1) << "ate : ";
cin >> pancakes[count];
}

cout << "Person" << (count + 1) << "ate : ";

should be
cout << "Person" << (count) << "ate : ";

also you forgot to use a { here
for (count= 0; count < numPeople; count++)

should be

for (count= 0; count < numPeople; count++) {


Last edited on
Topic archived. No new replies allowed.