我想知道是否有人能帮我解决这个问题。我希望在C++的一个学生管理结果分析项目中获得最低分。我得到一个“表达式必须有指向对象类型的指针”错误。我不知道该怎么解决这个问题
struct candidate
{
int candidates;
char forename[20], surname[20];
int area[5];
double avg;
public:
void getdata();
string calculateGrade();
void showdata() const;
};
//Prints out the lowest mark
void lowestmark(double avg[])
{
candidate st;
ifstream inFile;
inFile.open("student.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open! Press any Key...";
cin.ignore();
cin.get();
return;
}
while (inFile.read(reinterpret_cast<char *> (&st), sizeof(candidate)))
{
double smallest = st.avg;
// Loop to determine lowest score
for (int i = 0; i < sizeof(candidate); i++)
{
if (smallest > st.avg[i])
{
smallest = st.avg[i];
smallest = i;
}
}
}
}发布于 2017-01-20 02:58:29
你不需要数组就能找到最小的平均分数:
double smallest_average = 100000.0;
candidate st;
while (infile.read((unsigned char *) &st, sizeof(st)))
{
const double average_read = st.avg;
if (average_read < smallest_average)
{
smallest_average = average_read;
}
}搜索不需要存储到内存中的所有项目;只需要从文件中读取当前项目。
https://stackoverflow.com/questions/41748999
复制相似问题