gpt4 book ai didi

c++ - C++ 总分

转载 作者:太空宇宙 更新时间:2023-11-04 11:36:34 24 4
gpt4 key购买 nike

好吧,当我运行这段代码时,我得到我的总和等于 0 并且弄乱了我的平均水平和成绩。我不确定我做错了什么total += scores 函数在它应该在的地方,但它仍然没有加起来分数。

int validateNumber(int, int, int);

main() 函数中

int num, score, total = 0;

validateNumber(num, score, total);

和定义

int validateNumber(int num, int score, int total) {
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
return total;
}

最佳答案

如果你想像这里那样实现validate()函数,

validateNumber(num,score,total);

您可以将其设置为 void 并传递变量 total 作为引用。例如,

void validateNumber(int num, int score, int &total) {
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
}

其余的都是一样的...否则我不会在这种情况下使用 3 个参数。例如,

int validateNumber(int num) {
int total=0,score;
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
return total;
}

和调用:

int num, total;
...
total=validateNumber(num);

希望对您有所帮助...

关于c++ - C++ 总分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22885300/

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