需要帮助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;
}
需要考虑的几点:
-
你的成绩分配应该考虑
average
而不是marks
。如果你使用标记,那么最后输入的值是-1
,以退出你的do while循环,将用于评分,这将始终导致F级。 -
在
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
。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址