【问题标题】:Need help do-while loop program? (C++)需要帮助 do-while 循环程序? (C++)
【发布时间】:2011-10-28 03:02:57
【问题描述】:

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

有什么建议吗? 编辑:也许它在计算中,因为我的输出总是将百分比作为“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;
}

【问题讨论】:

    标签: c++ average do-while


    【解决方案1】:

    需要考虑的几件事:

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

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

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

    【讨论】:

    • 对于你们的第一部分建议,当我的平均计算出现在 do-while 循环之后时,我该如何实现?我在编译器中尝试过,结果没有输出?!谢谢
    • 这对我来说似乎是家庭作业。我不会提供代码。如果您的编辑器支持调试,请跳过您的代码并仔细观察变量。如果没有,请添加调试语句以查看哪里出错了。
    • 我是调试器的新手,但经过几次尝试掌握它后,我设法做到了,并意识到我的可怜错误 D:感谢您的耐心等待,非常感谢!跨度>
    【解决方案2】:

    仔细检查您的 if 声明。我敢打赌,您总是看到正确的平均值相当于 F。

    【讨论】:

    • 你会怎么做才能阻止我的正确平均值总是'F'?我不确定如何将平均值与下面的 if 语句进行比较,因为在我的平均值计算中是在我的 while 语句之后?
    【解决方案3】:

    在循环结束时,mark 始终为 -1。所以所有的测试都失败了,你进入else子句,并输出一个F。修复是测试average,而不是mark

    【讨论】:

    • 当我的平均值计算在我的循环之外,因此无法测试时,我如何测试平均值?
    • 不,我的意思是在输出平均值的if/else if 树中。
    • if/else if 树用于确定用户输入是否有效.... if 语句正在测试用户是否输入了有效标记。我不确定我为什么要测试取而代之的是平均值?
    • 首先,抱歉,我知道你的意思,一个非常愚蠢的错误。感谢您的耐心等待,非常感谢!
    猜你喜欢
    • 2013-05-24
    • 1970-01-01
    • 2010-11-02
    • 2023-02-19
    • 2016-11-30
    • 1970-01-01
    • 2018-05-09
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多