0

ユーザー入力(マーク)を取得し、これらのマークの平均をレターグレードに出力するプログラムを作成しようとしています。プログラムが平均を正常に計算することに成功したと思いますが、正しい文字の等級が出力されませんか?

助言がありますか?編集:私の出力は常にパーセンテージを「F」グレードとして与えるため、おそらく計算上の何かです....

// Used iomanip to display averages for possible three digit or greater answers.
#include <iostream>
#include <iomanip>
using namespace std;

// Set up my program using only main()
int main()
{
    //Created the variable 'mark' so the user input can be stored and tested in the do-while condition.
    //Variable 'sum' and 'average' to calculate mark and then be tested against letter grades.
    //'counter' used to keep track of number of terms being averaged.
    double mark=0, sum=0, average=0;
    int counter=-1;

    // Do-while statement to test the loop. Gets input from user, and tests whether it is a a valid letter grade.
    // Marks below 0 or above 100 will test true and loop back re-promting the user for another mark.
    // If tested condition is false, then if statements then test the mark and output the letter grade.

    cout << "Please enter marks to calculate average, and enter -1 to stop and find letter equivalence. " << endl;

    do
    {
        if ( mark < 0 || mark > 100 )
        {
            cout << "Entered mark is invalid. Please try again, or enter -1 to stop and find letter equivalence. " << endl;
            break;
        }

        sum = sum + mark;
        counter++;
        cin >> mark;

    }
    while ( mark != -1 );

    average = sum / counter ;

    //What happens when above statement is false...
    if (mark >= 90)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A+ ";
        }
    else if (mark >=80)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A ";
        }
    else if (mark >=70)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of B ";
        }
    else if (mark >=60)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of C ";
        }
    else if (mark >=50)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of D ";
        }
    else
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of F ";
        }
return 0;
}
4

3 に答える 3

1

考慮すべきいくつかの事柄:

  1. 成績の割り当てでは、を考慮する必要がaverageありmarksます。マークを使用する場合-1、do whileループを終了する最後に入力された値が評価に使用され、常にFグレードになります。

  2. ループの後endlにすべてのcoutステートメントに追加します。do-while出力ストリームがバッファリングされている可能性があり、これによりcoutステートメントがモニターに表示されない可能性があります。endl出力ストリームをフラッシュします。

たとえば。
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A+ " << endl;

于 2011-10-28T03:07:40.020 に答える
0

ifステートメントを再確認してください。常に正しい平均がFに相当することを確認していると思います。

于 2011-10-28T03:08:16.980 に答える
0

ループの最後でmarkは、常に-1. したがって、すべてのテストが失敗し、else句に落ちて F が出力されます。修正はaverage、 ではなくをテストすることmarkです。

于 2011-10-28T03:19:24.567 に答える