We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,687 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Score is corrupted

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;
}
3
Contributors
4
Replies
2 Days
Discussion Span
11 Months Ago
Last Updated
6
Views
Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
Skill Endorsements: 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.

Schol-R-LEA
Veteran Poster
1,019 posts since Oct 2010
Reputation Points: 414
Solved Threads: 163
Skill Endorsements: 10

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

Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
Skill Endorsements: 0

Didn't work. =////

Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
Skill Endorsements: 0

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

DeanMSands3
Posting Whiz
310 posts since Jan 2012
Reputation Points: 80
Solved Threads: 42
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0787 seconds using 2.89MB