Why is my score corrupted???

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    string names[10];
    int score[10];

    for(int scores = 10; scores >= 0; scores--)
    {
        cout << "What is your score? ";
        cin >> score[scores];
        cout << endl;
    }


    int swapHolder = -1;
    int ten = 10;

    for(int counter = 9; counter >= 0; counter--)
    {
        for(int i = 0; i < 10; i++)
        {
            if(score[i] > score[i + 1])
            {
                swapHolder = score[i + 1];
                score[i + 1] = score[i];
                score[i] = swapHolder;
            }
        }
        ten--;
    }

    for(int i = 10; i >= 0; i--)
    {
        cout << "Score: " << score[i] << endl;
    }


    system("pause");
    return 0;
}

How is it getting corrupted? Just what is the problem behavior?

Now, I should mention that the way you have the for() loops indexed is potentially problematic, because you are starting at 10 on each of them. Why would this be a problem? Because a ten-element array would have indices from 0 to 9, not 0 to 10. The result is that, from the very start, you are walking off of the end of the array. If you change the initialization so that it begins at 9, rather than 10 - or better still use a named constant - you should avoid this problem.

const int SCORE_COUNT = 10;

int main()
{
    string names[SCORE_COUNT];
    int score[SCORE_COUNT];

    for(int scores = SCORE_COUNT - 1; scores >= 0; scores--)
    {
        cout << "What is your score? ";
        cin >> score[scores];
        cout << endl;
    }

Of course, it would be more conventional to do it like this:

    for(int scores = 0; scores > SCORE_COUNT; scores++)
    {
        cout << "What is your score? ";
        cin >> score[scores];
        cout << endl;
    }

but I gather you have some particular need for the scores to be stored in reverse order.

Yes. I need the scores to be in decending order. Like greatest to smallest. I want to do this, just to see if I can. You know? But Thank You for posting this. I am going to try this way and see if this way works. Thank You Schol-R-LEA

Didn't work. =////

Lines 24 and 26.
You've got a for loop variable i going from 0 to 9. You've got a comparison between score[i] and score[i+1]
When i hits 9, that's a comparison between score[9] and score[10].
Silly question for the day: Does score[10] exist?

Happy coding.
-dean

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.