簡體   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