我正在用C ++创建一个简单的成绩计算器,我只是C ++的新手,我正在练习它,但是我的方法或代码有问题,希望您能为我提供帮助一。我不知道是否可以在for循环方法中添加用户输入(使用浮点数)并打印出来。谢谢,新年快乐!
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float sum = 0.00, avg, grades [10], i;
cout << "\n Enter your grades on each subject\n";
cout << "-----------------------------------------------";
cout << "\n Chemistry for Engineers Laboratory: ";
cin >> grades [0];
cout << "\n Chemistry for Engineers: ";
cin >> grades [1];
cout << "\n Computer Aided Drafting: ";
cin >> grades [2];
cout << "\n Calculus";
cin >> grades [3];
cout << "\n Kontekstwalisadong Komunikasyon sa Filipino at sa Iba't-ibang Larangan: ";
cin >> grades [4];
cout << "\n Science Technology and Society: ";
cin >> grades [5];
cout << "\n Mathematics in the Modern World: ";
cin >> grades [6];
cout << "\n Movemement Enhancement (Physical Education): ";
cin >> grades [7];
cout << "\n Readings in Philippine History: ";
cin >> grades [8];
cout << "\n Undertanding the Self: ";
cin >> grades [9];
for ( i = 0; i < 10; i++) {
printf ("%.1f + %.1f\n", sum , avg, sum+avg);
}
cout << "----------------------------------------";
cout << "Total Grade: " << sum;
avg = sum / 10;
cout << "\n GWA: " << avg;
cout << "\n Rating: ";
if ( avg > 1.00) {
cout << "Excellent";
} else if ( avg > 3.00 && avg <= 2.00) {
cout << "Passed";
} else if ( avg > 4.00 && avg <= 3.00) {
cout << "Conditional Passing";
} else {
cout << "Failed";
}
return 0;
}
您不能使用printf
计算成绩总和。您可以像这样使用循环来计算sum
:
for (i = 0; i < 10; i++) {
sum += grades[i];
}
[printf
是一个库函数,它将格式化的输出发送到标准输出(您的屏幕)。
for(i=0; i<10; i++) {
sum += grades[i];
// equals to `sum = sum + grades[i];`
}