简体   繁体   English

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

[英]My program keeps rounding off to nearest integer

I was having an annoying issue in my C++ lab today that I couldn't figure out.我今天在我的 C++ 实验室遇到了一个烦人的问题,我无法弄清楚。 My class is learning about switch statements and while loops, and are working on a project to input grades and calculate grade-point-averages.我的 class 正在学习 switch 语句和 while 循环,并且正在研究一个输入成绩和计算平均成绩的项目。 My program seems to be coming along well, but I can't figure out why it keeps rounding up to a whole integer instead of displaying decimal points.我的程序似乎进展顺利,但我不明白为什么它一直四舍五入到整个 integer 而不是显示小数点。 For example, instead of outputting something like 2.76, it'll output 3.00 as the GPA when I input random grades.例如,当我输入随机成绩时,它将 output 3.00 作为 GPA,而不是输出 2.76 之类的东西。 My code is still a work in progress but it should be able to calculate decimal numbers when I turn it in like my professor's sample program: http://classes.aligra.com/Riverside%20City%20College/2019%20Fall/CSC5/Projects/Project%2002.pdf我的代码仍在进行中,但是当我像教授的示例程序一样上交它时,它应该能够计算十进制数: http://classes.aligra.com/Riverside%20City%20College/2019%20Fall/CSC5/项目/项目%2002.pdf

Any feedback would be greatly appreciated.任何反馈将不胜感激。

#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;
    }

Your total_gp and num_of_grades are both int .您的total_gpnum_of_grades都是int Division of two integers is an integral operation, resulting in an int (where the rounding occurs).两个整数的除法是一个整数运算,产生一个int (舍入发生的地方)。 This integer is then assigned to a double variable gpa , but the damage is done already — there is no way to recover the lost information.然后将此 integer 分配给double变量gpa ,但损坏已经造成 - 无法恢复丢失的信息。

You need to hint to the compiler that you want the floating-point division, by making sure at least one of the operands is of a floating-point type.您需要通过确保至少有一个操作数是浮点类型来向编译器提示您需要浮点除法。 Either of these will work:这些中的任何一个都可以工作:

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

Modify your case 'x' like this像这样修改你的案例'x'

gpa = (float) total_gp/num_of_grades

Because two integer division will always return integer.因为两个 integer 除法总是会返回 integer。

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

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