-3

このプログラムを実行すると、「Run-Time Check Failure #2 stack around the variable 'numGrades' was modified」が表示されます。また、最下位は正解を出力しません。どんな助けでも大歓迎です!

#include <iostream> // cin, cout

using namespace std;

const int TEN_GRADES = 10;   // pre-defined number of grades

int main()
{
    int numGrades[10];
    double avg, highest = numGrades[0], lowest = numGrades[0], less, greater, grades;
    double sum = 0;

    // greeting message
    cout << "---------------------------------" << endl
         << "  Sandro's Statistics Generator  " << endl
         << "---------------------------------" << endl << endl;

    // requesting number of grades 
    cout << "Hello Professor, how many grades do I need to analyse this time? ";
    cin >> numGrades[10];

    if (numGrades[1] == 0)
    {
        cout << "\nGuess you changed your mind!!!" << endl
             << "Ending program now..." << endl << endl;
        return 0;
    }

    // if user doesn't enter 0 user is ready to begin
    cout << "Okay, I am ready. Start..." << endl;
    for (int count = 0; count < numGrades[10]; count++)
    {
        cin >> numGrades[count];
        sum += numGrades[10];
    }

    // to get the average
    avg = sum / TEN_GRADES;

    // to get the highest and lowest mark

    for (int count = 0; count < TEN_GRADES; count++)
    {
        if (numGrades[count] > highest)
            highest = numGrades[count];
    }

    for (int count = 0; count < TEN_GRADES; count++)
    {
        if (numGrades[count] < lowest)
            lowest = numGrades[count];
    }

    // output requested statistics
    cout << "Here are the requested stats for the " << numGrades << " grades."     << endl
         << "The class average is " << avg << endl
         << "The highest grade is " << highest << endl
         << "The lowest grade is " << lowest << endl;

    return 0;

}

4

1 に答える 1

0

なんてこった、私はC ++すらやっていないのですが、片方の目が少し出血したと思います.

それらに値を作成して割り当てる方法を確認してください (または、これをコーディングした人に確認するように伝えてください)。

次に、単純なデータ構造 (配列など) とループを確認します。

開始する良い方法の 1 つは、プログラムの次の WORKING コードを分析することです。

助けになる場合は正しいとマークしてください。質問がある場合は... 乾杯!

#include <iostream>
#include <string>

using namespace std;

const int TEN_GRADES = 10;   // pre-defined number of grades

int main()
{
int numGrades[10];
double avg, highest = 0, lowest = 0, less, greater, grades;
double sum = 0;

// greeting message
cout << "---------------------------------" << endl
     << "  Newbie Statistics Generator  " << endl
     << "---------------------------------" << endl << endl;

// requesting number of grades 
cout << "Hello Professor, please enter 10 grades: "<<endl;

//THIS PART: loops ten times to input the grades
for (int count = 0; count < TEN_GRADES; count ++)
{
    cout << "Grade number "<<count<<":";
    cin >> numGrades[count];

}

//I get what you want to do here, but consider adding another exit condition here, what if the second grade is really 0 ?
if (numGrades[1] == 0)
{
    cout << "\nGuess you changed your mind!!!" << endl
         << "Ending program now..." << endl << endl;
    return 0;
}

// if user doesn't enter 0 user is ready to begin
cout << "Okay, I am ready. Start..." << endl;
for (int count = 0; count < TEN_GRADES; count++)
{
    sum += numGrades[count];
}

// to get the average
avg = sum / TEN_GRADES;

// to get the highest and lowest mark
for (int count = 0; count < TEN_GRADES; count++)
{
    if (numGrades[count] > highest)
        highest = numGrades[count];
}

for (int count = 0; count < TEN_GRADES; count++)
{
    if (numGrades[count] < lowest)
        lowest = numGrades[count];
}
// output requested statistics
cout << "Here are the requested stats for the " << TEN_GRADES << " grades."     << endl
     << "The class average is " << avg << endl
     << "The highest grade is " << highest << endl
     << "The lowest grade is " << lowest << endl;

return 0;
}
于 2016-11-14T01:00:20.467 に答える