Pancake Glutton exercise --- 1 issue

If your not familiar with it:

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.

So after much frustration I have this step complete. The problem is it sometimes gives the proper output of which number ate the most but sometimes it is wrong.

Usually its correct but theres some situations that bug it up, I'll post some examples at the bottom.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main()
{
    int myArray[9];
    int maxEaten;
    int maxPerson;

    cout << "So how many waffles did each person eat? \n";
    for(int a = 0; a < 10; a++){
        cout << "Person " << a + 1 << " ate: ";
        cin >> myArray[a];
    }

    maxEaten = myArray[0];

    for(int b = 0; b < 10; b++){
        if(maxEaten < myArray[b]){
            maxEaten = myArray[b];
            maxPerson = b + 1;
        }
    }

    cout << "Person " << maxPerson << " ate the most waffles: " << maxEaten << "\n";


    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
So how many waffles did each person eat?
Person 1 ate: 1
Person 2 ate: 1
Person 3 ate: 1
Person 4 ate: 1
Person 5 ate: 1
Person 6 ate: 1
Person 7 ate: 1
Person 8 ate: 1
Person 9 ate: 1
Person 10 ate: 10
Person 10 ate the most waffles: 9

Process returned 0 (0x0)   execution time : 8.486 s
Press any key to continue.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
So how many waffles did each person eat?
Person 1 ate: 9
Person 2 ate: 8
Person 3 ate: 7
Person 4 ate: 6
Person 5 ate: 5
Person 6 ate: 4
Person 7 ate: 3
Person 8 ate: 2
Person 9 ate: 1
Person 10 ate: 10
Person -2 ate the most waffles: 9

Process returned 0 (0x0)   execution time : 7.847 s
Press any key to continue.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
So how many waffles did each person eat?
Person 1 ate: 5
Person 2 ate: 8
Person 3 ate: 9
Person 4 ate: 6
Person 5 ate: 4
Person 6 ate: 2
Person 7 ate: 7
Person 8 ate: 8
Person 9 ate: 0
Person 10 ate: 10
Person 3 ate the most waffles: 9

Process returned 0 (0x0)   execution time : 26.099 s
Press any key to continue.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
So how many waffles did each person eat?
Person 1 ate: 12
Person 2 ate: 51
Person 3 ate: 48
Person 4 ate: 62
Person 5 ate: 14
Person 6 ate: 52
Person 7 ate: 32
Person 8 ate: 84
Person 9 ate: 15
Person 10 ate: 101
Person 8 ate the most waffles: 84

Process returned 0 (0x0)   execution time : 23.868 s
Press any key to continue.


I kept playing with it to gets the above shots and it seems if Person 10 ever eats the most it ignores 10 and outputs the person who ate the second most.
You don't have 10 people. You have 9 people.

Change line 7 to int myArray[10] ;. You are currently clobbering memory you don't own, and your code is not at all happy with you.
Ah was confused by the way the array uses 0-9 as the 10 people. Thank you.
Topic archived. No new replies allowed.