>a)){ cout,c++,C++" /> >a)){ cout,c++,C++" />

我计算最终成绩的程序没有';我不能计算它,我可以';我不知道为什么 我一直在尝试编写一个C++程序来计算你的年终成绩(谷歌为教育C++课程提供的一个练习)。该程序运行正常,除了它没有计算你的最终成绩,相反,它只输出“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; } #包括 使用名称空间std; 整数检查(整数a){ 如果(!(cin>>a)){ cout

我计算最终成绩的程序没有';我不能计算它,我可以';我不知道为什么 我一直在尝试编写一个C++程序来计算你的年终成绩(谷歌为教育C++课程提供的一个练习)。该程序运行正常,除了它没有计算你的最终成绩,相反,它只输出“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; } #包括 使用名称空间std; 整数检查(整数a){ 如果(!(cin>>a)){ cout,c++,C++,要么将值作为引用传递给check(),要么进行检查以返回输入值 改变 int check(int a) 到 第二种方法 将支票修改为 int check(int a) { if (!(cin >> a)) { cout << "Come on, that isn't a score" << endl; return a; } } int检查(int a){ 如果(!(cin>>a)){ cout您的cin>>

要么将值作为引用传递给check(),要么进行检查以返回输入值

改变

int check(int a)

第二种方法

将支票修改为

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

cout您的
cin>>a
语句更新本地变量的值,该值在
check()
返回时立即消失。您希望更新实际用于计算分数的变量的值。只需将函数
check()
更改为按引用传递
check(int&a)
或传递指针
check即可(int*a)

check(int*a)
更改为
check(int&a)
,它应该可以工作。您只需在
check
中更改局部变量
a
。也许您的意思是
检查(int*a)
cin>*a
检查(&assignment1)
您似乎期望
int
类型的变量以某种方式表示分数。您知道“整数”这个词是什么吗意思?下定决心。你想让函数
检查
返回值还是不返回值?而且它肯定不能更改通过值传递的变量的值。你没有尽可能多地创建函数。
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);