Вопрос

I am new to C++ and have written some code in which I'm getting the following error:

Run-Time Check Failure #2 - Stack around the variable 'scores' was corrupted

What is causing this error?

Here is my code:

#include <iostream> // Enables cout and endl
#include <string>
#include <sstream>
#include "stdafx.h"

using namespace std;
int getInput();

int main()
{
    int scores[5];
    int i;
    int j;
    int numberOfScores;

    for (i = 0; i < 6; i++) // Sets all 5 elements of the array to zero
    {
        scores[i] = 0;
    }

    cout << "How many scores do you have to enter?\n" << endl;
    cin >> numberOfScores;


    for (j = 0; j < numberOfScores; j++) // Gather test scores and increases each array index as that score is entered
    {
        scores[getInput()] ++;
    }

    cout << "The number of zeros: " << scores[0] << endl;
    cout << "The number of ones: " << scores[1] << endl;
    cout << "The number of twos: " << scores[2] << endl;
    cout << "The number of threes: " << scores[3] << endl;
    cout << "The number of fours: " << scores[4] << endl;
    cout << "The number of fives: " << scores[5] << endl;


    return 0;
}

int getInput()
{
    int enteredScore;
    cout << "Enter the test scores one at a time.\n";
    cout << "The range of scores is 0 to 5.\n";
    cin >> enteredScore;

    if (enteredScore >= 0 && enteredScore <= 5)
    {
        return enteredScore;
    }
    else
    {
        cout << "Error!  The range of scores is 0 to 5.\n";
        cout << "Enter the test scores one at a time.\n";
        cin >> enteredScore;
        return enteredScore;
    }
}
Это было полезно?

Решение

It seems that this declaration:

int scores[5];

Is incorrect. This creates an array with 5 numbers in it, indices from scores[0-4], however, you constantly refer to score[5], the sixth element of the array throughout your program. I recommend changing to

int scores[6];

Другие советы

The problem:

You are accessing your array out of bounds in several places.

Here you loop through 6 elements when you only have 5:

for (i = 0; i < 6; i++) // Loops through 6 elements
    {
        scores[i] = 0;
    }

Here you call getInput() and use the return value as the index:

scores[getInput()] ++;

However, the first half of the function accepts inputs from the user in the range 0 to 5, thus allowing access to 6 elements:

if (enteredScore >= 0 && enteredScore <= 5)

It gets even worse if the user enters a number outside that range, as they are then given a second opportunity to enter a number, only this time there is no validation and any number they enter is accepted:

cin >> enteredScore;
return enteredScore;

Finally, you again attempt to access a 6th element here:

cout << "The number of fives: " << scores[5] << endl;

Solution:

First, you need to do one of two things:

  • Change the for loop, if statement, and cout statements so that they do not access index 5

or:

  • Create the array so that it has 6 elements: int scores[6];

Secondly, you need to fix the bug in your getInput() function so that it validates the input properly. You could try this for example:

int getInput()
{
    int enteredScore;
    cout << "Enter the test scores one at a time.\n";
    cout << "The range of scores is 0 to 4.\n";
    cin >> enteredScore;

    while (enteredScore < 0 || enteredScore > 4)
    {
        cout << "Error!  The range of scores is 0 to 4.\n";
        cout << "Enter the test scores one at a time.\n";
        cin >> enteredScore;
    }
    return enteredScore;
}

You have an error in

 cout << "The number of fives: " << scores[5] << endl;

Your array is of size 5 but you are accessing the 6th element.

Same with for (i = 0; i < 6; i++) should be i < 5.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top