C++構造体に問題があります。下記の私のプログラムでは、ファイルから質問の数、試験への回答、試験への回答者のファイルを読み込もうとしています。それはすべて動作しますが、学生の情報を構造体の配列に入れようとすると、何らかの理由で変数idが機能しません。 Microsoft Visual Studio 2017 RCのコンパイラでは、「students-> id [i]」に「式にオブジェクト型へのポインタが必要です」というエラーがあり、その理由がわかりません。私は問題がどこにあるのかをマークし、コードの残りの部分を取り除きました。私が持っているのは、calculateGrade関数が使われています。私はこれをしばらくは続けてきましたが、これを修正することなくどこにでも行くことはできません。どんな助けもありがとう!C++構造体オブジェクト型エラーへのポインタ
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct studentInfo {
int id;
string firstName;
string lastName;
string exam;
};
double calculateGrade(struct studentInfo);
int main() {
const int SIZE = 12;
studentInfo students[SIZE];
string fileName, key, studentFile;
ifstream file, fileForStudents;
int numOfQuestions, i = 0, id;
cout << "Please enter a file name: ";
cin >> fileName;
file.open(fileName);
if (!file.is_open()) {
cout << "Could not open file";
}
file >> numOfQuestions >> key >> studentFile;
fileForStudents.open(studentFile);
if (!fileForStudents.is_open()) {
cout << "Could not open file";
}
while (!fileForStudents.eof()) {
fileForStudents >> id >> students->firstName[i] >> students->lastName[i] >> students->exam[i];
students->id[i] = id; //issue is here
i++;
}
calculateGrade(students[SIZE]);
return 0;
}
大文字のC++で定数を持つことは、アンチパターンです。 – Slava
['while(!file.eof())'が間違っている](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Barmar