我的程序不断四舍五入到最接近的整数
My program keeps rounding off to nearest integer
我今天在C++实验室遇到了一个烦人的问题,我无法弄清楚。我的班级正在学习开关语句和 while 循环,并且正在研究一个输入成绩和计算平均绩点的项目。我的程序似乎进展顺利,但我无法弄清楚为什么它一直四舍五入到整数而不是显示小数点。例如,当我输入随机成绩时,它会输出 3.00 作为 GPA,而不是输出 2.76 之类的东西。我的代码仍在进行中,但是当我像教授的示例程序一样上交它时,它应该能够计算十进制数:http://classes.aligra.com/Riverside%20City%20College/2019%20Fall/CSC5/Projects/Project%2002.pdf
任何反馈将不胜感激。
#include <iostream>
#include <iomanip>
using namespace std;
/*******************************************************
*
* COMPUTE GRADE POINT AVERAGE
* _____________________________________________________
* This program accepts as user input from an instructor
* to calculate his/her class's grade point average.
* This will be done through the use of the Do-While
* loop and if-else statments.
* _____________________________________________________
* INPUT
* score : A grade of one of the instructor's students.
*
* OUTPUT
* score : A grade of one of the instructor's students.
*
********************************************************/
int main()
{
/******************************************************
* CONSTANTS
* ____________________________________________________
* A_score : Variable for a grade of A.
* B_score : Variable for a grade of B.
* C_score : Variable for a grade of C.
* D_score : Variable for a grade of D.
*****************************************************/
const int A_score = 4;
const int B_score = 3;
const int C_score = 2;
const int D_score = 1;
char score;
int count = 1;
int num_of_grades = 0;
int total_gp = 0;
double gpa;
cout << "TEST #" << count << ":nn";
do {
cout << setw(45) << "Enter Letter Grade (enter 'X' to exit): ";
//cin.ignore();
cin >> score;
//cin.get(score);
switch (score)
{
case 'A': num_of_grades += 1;
total_gp += A_score;
break;
case 'a': num_of_grades += 1;
total_gp += A_score;
break;
case 'B': num_of_grades += 1;
total_gp += B_score;
break;
case 'b': num_of_grades += 1;
total_gp += B_score;
break;
case 'C': num_of_grades += 1;
total_gp += C_score;
break;
case 'c': num_of_grades += 1;
total_gp += C_score;
break;
case 'D': num_of_grades += 1;
total_gp += D_score;
break;
case 'd': num_of_grades += 1;
total_gp += D_score;
break;
case 'F': num_of_grades += 1;
total_gp += 0;
break;
case 'f': num_of_grades += 1;
total_gp += 0;
break;
case 'X': gpa = total_gp/num_of_grades;
cout << "nnTotal Grade Points: " << total_gp;
cout << "nGPA: " << gpa << "nn";
count += 1;
cout << "nTEST #" << count << ":nn";
break;
case 'x': gpa = total_gp/num_of_grades;
cout << "nnTotal Grade Points: " << total_gp;
cout << "nGPA: " << setprecision(2) << fixed << gpa << "nn";
count += 1;
cout << "nTEST #" << count << ":nn";
break;
default: cout << "n" << setw(45) << "Invalid letter grade, please try againnn";
}
} while (count < 4);
return 0;
}
您的total_gp
和num_of_grades
都是int
。两个整数的除法是一个积分运算,导致int
(四舍五入发生的地方(。然后将这个整数分配给一个double
变量gpa
,但损害已经造成 - 没有办法恢复丢失的信息。
您需要通过确保至少一个操作数是浮点类型来向编译器提示您想要浮点除法。以下任一方法均有效:
gpa = (double) total_gp/num_of_grades;
gpa = total_gp/(double) num_of_grades;
像这样修改大小写"x">
gpa = (float) total_gp/num_of_grades
因为两个整数除法将始终返回整数。
相关文章:
- 查找最接近的大于当前数字的数字的索引
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 查找索引的最接近的真实值
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- C++ lower_bound() 搜索最接近目标值的元素
- C++有效地找到向量中第一个最接近的匹配值?
- 在 c++ 中,能够将浮点类型加 1 的最大整数是多少?
- 在结构向量中查找最接近的值
- 查找整数数组中最接近的数字
- 我的程序不断四舍五入到最接近的整数
- 将整数舍入到另一个整数的最接近的倍数
- 如果我想将一个无符号整数四舍五入到最接近的更小或等于偶数的整数,我可以除以2然后乘以2吗
- 最接近 C++03 中浮点值的整数
- 取浮点数最接近的整数次幂
- 将整数移动到最接近的可被 4 整除的 C++
- 求最接近整数n的2^i值
- 寻找QT函数将qint64类型的变量(整数)四舍五入到最接近的十
- 如何四舍五入的总成本(双)到最接近的整数
- 整数boost::multiprecision::mpq_rational到最接近的整数
- 如何有效地找到最接近两个整数以100,000为增量的平均值的最大整数