繁体   English   中英

我的程序不断四舍五入到最近的 integer

[英]My program keeps rounding off to nearest integer

我今天在我的 C++ 实验室遇到了一个烦人的问题,我无法弄清楚。 我的 class 正在学习 switch 语句和 while 循环,并且正在研究一个输入成绩和计算平均成绩的项目。 我的程序似乎进展顺利,但我不明白为什么它一直四舍五入到整个 integer 而不是显示小数点。 例如,当我输入随机成绩时,它将 output 3.00 作为 GPA,而不是输出 2.76 之类的东西。 我的代码仍在进行中,但是当我像教授的示例程序一样上交它时,它应该能够计算十进制数: http://classes.aligra.com/Riverside%20City%20College/2019%20Fall/CSC5/项目/项目%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 << ":\n\n";
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 << "\n\nTotal Grade Points: " << total_gp;
        cout << "\nGPA: " << gpa << "\n\n";
        count += 1;
        cout << "\nTEST #" << count << ":\n\n";
        break;

        case 'x': gpa = total_gp/num_of_grades;
        cout << "\n\nTotal Grade Points: " << total_gp;
        cout << "\nGPA: " << setprecision(2) << fixed << gpa << "\n\n";
        count += 1;
        cout << "\nTEST #" << count << ":\n\n";
        break;

        default: cout << "\n" << setw(45) << "Invalid letter grade, please try again\n\n";
    }


    } while (count < 4);

    return 0;
    }

您的total_gpnum_of_grades都是int 两个整数的除法是一个整数运算,产生一个int (舍入发生的地方)。 然后将此 integer 分配给double变量gpa ,但损坏已经造成 - 无法恢复丢失的信息。

您需要通过确保至少有一个操作数是浮点类型来向编译器提示您需要浮点除法。 这些中的任何一个都可以工作:

gpa = (double) total_gp/num_of_grades;
gpa = total_gp/(double) num_of_grades;

像这样修改你的案例'x'

gpa = (float) total_gp/num_of_grades

因为两个 integer 除法总是会返回 integer。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM