gpt4 book ai didi

c++ - 我计算最终成绩的程序不计算它,我也不知道为什么

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:17:53 26 4
gpt4 key购买 nike

我一直在尝试编写一个 C++ 程序来计算您的年终成绩(Google for Education C++ 类(class)提供的练习)。该程序有效,除了它不计算你的最终成绩,而是只输出“0”。我已经搜索了代码,但似乎找不到问题。

#include <iostream>
using namespace std;

int check(int a) {
if (!(cin >> a)) {
cout << "Come on, that isn't a score" << endl;
return 0;
}
}

int assignments() {
int assignment1 = 0;
int assignment2 = 0;
int assignment3 = 0;
int assignment4 = 0;

cout << "Enter the score for the first assignment. ";
check(assignment1);
cout << "Enter the score for the second assignment. ";
check(assignment2);
cout << "Enter the score for the third assignment. ";
check(assignment3);
cout << "Enter the score for the fourth assignment. ";
check(assignment4);
return ((assignment1 + assignment2 + assignment3 + assignment4) / 4 * 0.4);

}

int mid() {
int midterm = 0;
cout << "Enter the score for the midterm. ";
check(midterm);
return (midterm * 0.15);
}

int finalex() {
int finals = 0;
cout << "Enter the score for the final. ";
check(finals);
return (finals * 0.35);
}

int participation() {
int parti = 0;
cout << "Enter the class participation grade. ";
check(parti);
return (parti * 0.1);
}

int main() {
int assign = assignments();
int midt = mid();
int fingra = finalex();
int partigra = participation();
cout << "The final grade is: " << assign + midt + fingra + partigra << endl;
}

(我对每个等级类型都有不同程序的原因是因为类(class)规定你应该尽可能多地实现功能)

最佳答案

您应该将值作为引用传递给 check() 或进行检查以返回输入值。

改变

int check(int a)

int check(int& a)

Second method

修改检查为

int check(int a) {
if (!(cin >> a)) {
cout << "Come on, that isn't a score" << endl;
return a;
}
}

并使用返回值将输入分配给变量。喜欢

int midterm = 0;
cout << "Enter the score for the midterm. ";
midterm=check(midterm);

关于c++ - 我计算最终成绩的程序不计算它,我也不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30822743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com