簡體   English   中英

c ++ gpa計算器不接受成績輸入

[英]c++ gpa calculator not accepting grade input

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int course, numberOfClasses; //declare variables
    double gradePointTotal = 0, gradePointAve; //initialize to 0
    string grade;

    cout << "GPA Calculator \n";
    cout << "\n Enter the number of classes ";
    cin >> numberOfClasses; // enter number of classes

    for (course = 1; course <= numberOfClasses; course++ ) // define loop
    {
        cout << "\n Enter a letter grade for class number " << course << ": ";
        cin >> grade; //Enter grade
        if ( grade == "A" || grade == "a") //accepts upper and lower case
            gradePointTotal = gradePointTotal + 4;
        else if ( grade == "B" || grade == "b")
            gradePointTotal = gradePointTotal + 3;
        else if ( grade == "C" || grade == "c")
            gradePointTotal = gradePointTotal + 2;
        else if ( grade == "D" || grade == "d")
            gradePointTotal = gradePointTotal + 1;
        else if ( grade == "F" || grade == "f")
            gradePointTotal = gradePointTotal + 0;

        gradePointAve = gradePointTotal / numberOfClasses; // calculate the GPA
        cout << "\n Your GPA is: " << gradePointAve << endl; // display GPA

    }
}

我是 C++ 的新手。 我不確定為什么......但我的輸出不正確。 這個程序計算gpa。 我可以輸入我正在使用的課程數量,但是 - 我無法輸入字母等級。 我的行出錯:cin >> grade; 但我能夠通過添加 #include 來修復錯誤消息。 然而,它並沒有達到預期的效果.... 為什么在彈出控制台屏幕時我無法輸入我的字母等級?

gradePointAve = gradePointTotal / numberOfClasses; // wrong

是錯誤的,因為您會暗示 gradePointTotal 包含所有成績。 但由於您正在循環,情況並非如此。 您需要設置course ,而不是numberOfClasses作為商:

gradePointAve = gradePointTotal / course; // correct

至少這會給你正確的結果。

並刪除#include "stdafx.h" ,您不需要那個。 在您的項目文件夾中擁有標准庫並不是一件好事。 再說一次,代碼不需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM