【问题标题】:A value in my program is being calculated incorrectly, even though the supporting math is correct [duplicate]我的程序中的一个值计算不正确,即使支持数学是正确的[重复]
【发布时间】:2019-11-25 04:22:54
【问题描述】:

在我的班级的一个模块中,我们正在编写一个程序,询问班级名称、年级和学分数。在一定数量的课程结束时,它要求我们使用 setprecision(2) 将 GPA 计算到小数点后 2 位。我所有的程序功能除了,它计算 GPA 为 3.00 而不是 3.14。我的 totalPoints 达到 22,totalCredits 是 7,所以它应该是 3.14 与 setprecision。关于为什么我得到 3.00 的任何见解?感谢您提供任何帮助,因为我对此很陌生。

#include iostream
#include iomanip
#include string
//I took out the < and > because it made the words disappear

using namespace std;

int main(){
    cout<< std::fixed << std::setprecision(2);

    int totalCredits = 0;
    int gradePoints = 0;

    string courseName;
    cout<<"Enter a course name: ";
    getline (cin, courseName);
    cout<<courseName <<endl;

    int credits;
    cout<<"Enter number of credits: ";
    cin>>credits;
    cout<<credits <<endl;
    totalCredits = totalCredits + credits;

    string grade;
    cout<<"Enter your grade (A, B, C, D, F): ";
    cin>>grade;
    cout<<grade <<endl;

    if (grade == "A"){
      gradePoints = gradePoints + (4.00 * credits);
    }
    else if (grade == "B"){
      gradePoints = gradePoints + (3.00 * credits);
    }
    else if (grade == "C"){
      gradePoints = gradePoints + (2.00 * credits);
    }
    else if (grade == "D"){
      gradePoints = gradePoints + (1.00 * credits);
    }


    string answer;
    cout<<"Continue ('Yes' or 'No')? ";
    cin>>answer;
    cout<<answer <<endl;

    while (answer == "Yes"){
      cout<<"Enter a course name: ";
      cin.ignore();
      getline (cin, courseName);
      cout<<courseName <<endl;

      cout<<"Enter number of credits: ";
      cin>>credits;
      cout<<credits <<endl;
      totalCredits = totalCredits + credits;

      cout<<"Enter your grade (A, B, C, D, F): ";
      cin>>grade;
      cout<<grade <<endl;

      if (grade == "A"){
         gradePoints = gradePoints + (4.00 * credits);
      }
      else if (grade == "B"){
         gradePoints = gradePoints + (3.00 * credits);
      }
      else if (grade == "C"){
         gradePoints = gradePoints + (2.00 * credits);
      }
      else if (grade == "D"){
         gradePoints = gradePoints + (1.00 * credits);
      }


      cout<<"Continue ('Yes' or 'No')? ";
      cin>>answer;
      cout<<answer <<endl;
    }

    cout<<"Total grade points: " <<gradePoints <<endl;
    cout<<"Total credits attempted: " <<totalCredits <<endl;

   float gpa = 0;
   gpa = gradePoints/totalCredits;
   cout<<"Your GPA is " <<gpa <<endl;



   return 0;  
}

【问题讨论】:

    标签: c++ gpa


    【解决方案1】:

    int 除以 int 得到一个 int。通过除法语句在变量名前添加 (double) 来转换其中之一。


    其他注意事项:

    • 您几乎重复了整个程序,只是为了确保它执行一次。您只需将答案设置为“是”,您就知道它会执行。
    • cin getLine() 并根据需要进行解析more info。 (对于家庭作业可能有点矫枉过正......)在这种情况下,快速修复使用ignore() 清除流,并使用clear() 重置流。示例工作程序here

    【讨论】:

    • 当我尝试这样做时,它破坏了整个程序。我不知道如何将它从 int 更改为 double。
    • (double) gradePoints/totalCredits。其他一些提示:您正在重复几乎整个程序,只是为了确保它执行一次。这违背了编程的基础,你应该(几乎)永远不要重复自己。一种解决方案是执行一次的 do while 循环。我个人喜欢第二个选项:只需将答案设置为“是”,您就知道它会执行。恕我直言,干净得多。
    • 当您说一切都在中断时,这是由于cin 流在继续之前卡住而导致的无限循环。 (cin 在 c++ 中会变得非常挑剔)。编辑答案以包含此内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2015-01-26
    • 2010-11-21
    相关资源
    最近更新 更多