我的程序不断四舍五入到最接近的整数

My program keeps rounding off to nearest integer

本文关键字:最接近 整数 四舍五入 程序 我的      更新时间:2023-10-16

我今天在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_gpnum_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

因为两个整数除法将始终返回整数。