我正在尝试使用数组编写一个程序来存储6个测试的分数,然后在曲线上添加曲线,然后显示最终分数。我一直从Xcode得到一个错误,在for循环中有一个“指针与整数('int'和double'*')之间的比较”。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int test = 5; //sets the number of tests
double score[test]; //array to hold each tests score
double curve; //curve for the tests
double finalScore; //The final test scores after the curve is applied
//Input scores of the tests
cout << "Enter the scores for \n";
for (int test = 0; test < score; test++)
{
cout << "test #" << (test+1) <<": ";
cin >> score[test];
}
//input the curve
cout << "\nAll of these tests will have the same curve applied."
<< "\nEnter the curve: ";
cin >> curve;
//display the modified test scores
cout << "\nHere are the curve test scores:\n";
cout << fixed << showpoint << setprecision(2);
for (int test = 0; test < score; test++)
{
finalScore = score[test] + curve;
cout << "Test #" << (test + 1) << ": " << setw(7) << finalScore << endl;
}
return 0;
}
答案 0 :(得分:0)
double score[5]
定义了一个双精度数组,score
表示该数组的第一个元素的地址;因此,它是一个指针,int test=0; test < score
将积分值与指针值进行比较;因此错误/警告。
将nrOfTests
与用于迭代测试的变量区分开来,例如test
:
const int nrOfTests = 5; //sets the number of tests
double score[nrOfTests]; //array to hold each tests score
....
for (int test = 0; test < nrOfTests; test++) {
...
}
....
for (int test = 0; test < nrOfTests; test++) {
...
}