Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要帮助执行while循环程序吗?(C+;+;)_C++_Average_Do While - Fatal编程技术网

C++ 需要帮助执行while循环程序吗?(C+;+;)

C++ 需要帮助执行while循环程序吗?(C+;+;),c++,average,do-while,C++,Average,Do While,我正在尝试创建一个程序,它将接受用户输入(分数),并将这些分数的平均值输出到一个字母等级。 我相信我已经成功地让程序成功地计算出平均值,但是它不会输出正确的字母等级 有什么建议吗? 编辑:也许这是计算中的一些东西,因为我的输出总是给出百分比作为“F”级 // Used iomanip to display averages for possible three digit or greater answers. #include <iostream> #include <iom

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

有什么建议吗? 编辑:也许这是计算中的一些东西,因为我的输出总是给出百分比作为“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;
}
//使用iomanip显示可能的三位数或更高答案的平均值。
#包括
#包括
使用名称空间std;
//仅使用main()设置我的程序
int main()
{
//创建了变量“mark”,以便可以在do while条件下存储和测试用户输入。
//变量“总和”和“平均值”计算分数,然后根据字母等级进行测试。
//“计数器”用于跟踪平均项数。
双标记=0,和=0,平均=0;
int计数器=-1;
//Do while语句测试循环。从用户获取输入,并测试它是否为有效的字母等级。
//低于0或高于100的分数将测试为真,并循环返回,再次提示用户另一个分数。
//如果测试条件为false,则If语句测试标记并输出字母等级。
cout=90)
{

有两件事需要考虑:

<> L> >P>你的等级作业应考虑<代码>平均而不是<代码>标记< /代码>。如果使用标记,则最后输入的值(<代码> -1代码/代码>退出您的DoT while循环将被用于评分,这将总是导致F级。

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

  • 例如,

    cout仔细检查你的
    if
    语句。我敢打赌你总是看到正确的平均值等于F。在循环结束时,
    mark
    总是
    -1
    。因此所有测试都失败,你通过
    else
    子句,并输出一个F。解决办法是测试
    平均值,不是t
    mark

    如何阻止我的正确平均值始终为“F”?我不确定如何将平均值与下面的if语句进行比较,因为在我的平均值计算中,是在我的while语句之后?对于你建议的第一部分,当我的平均值计算在do while循环之后时,我如何实现这一点?我尝试了这一点在编译器中,结果是没有输出?!ThanksIt对我来说似乎是家庭作业。我不会提供代码。如果编辑器支持调试,请检查代码并仔细观察变量。如果不支持,请添加调试语句以查看错误所在。我对调试程序是新手,但尝试几次后,我管理了它D:谢谢你的耐心,真的很感激!当我的平均值计算超出我的循环,因此无法测试时,我如何测试平均值?不,我的意思是在输出平均值的
    if
    /
    else if
    树中。if/else if树用于确定是否使用r输入是有效的…if语句正在测试用户是否输入了有效的分数。我不确定为什么我会改为测试平均值?首先,抱歉,我知道你的意思,一个非常愚蠢的错误。谢谢你的耐心,非常感谢!