繁体   English   中英

需要“ do-while循环程序”帮助吗? (C ++)

[英]Need help do-while loop program? (C++)

我正在尝试创建一个程序,该程序将接受用户输入(标记)并将这些标记的平均值输出到字母等级。 我相信我已经成功地使程序成功地计算了平均值,但是它不会输出正确的字母等级?

有什么建议么? 编辑:也许这是计算中的某件事,因为我的输出始终将百分比表示为“ 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;
}

需要考虑的几件事:

  1. 您的成绩分配应考虑average而不是marks 如果使用标记,则最后一个输入的值-1用于退出do while循环)将用于评分,这将始终导致F评分。

  2. do-while循环之后,将endl添加到所有cout语句中。 输出流可能已被缓冲,这可能导致cout语句未出现在监视器上。 endl将刷新输出流。

例如。
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A+ " << endl;

仔细检查您的if语句。 我敢打赌,您总是会看到正确的平均值等于F。

在循环结束时, mark始终为-1 因此,所有测试均失败,您进入了else子句,并输出了F。解决方法是测试average而不是mark

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM