저는 C++을 처음 접했고 실수를 저에게 배웠습니다. 파일이 있고 파일 내에 다음 형식의 데이터가 포함되어 있습니다.파일에서 입력을 루프에 저장하고 배열 변수에 저장하는 방법은 무엇입니까?
"문자열", "문자열", 문자 및 숫자가 100 개 항목입니다. "Billy Joel A 96 Tim McCan B 70".
나는이 항목을 클래스 배열에 저장하려고합니다. 어쩌면 인스턴스 또는 개체를 의미 할 수도 있습니다.
이것은 내 나쁜 시도입니다. 다음 학생 정보를 얻지 못하는 이유는 ... 왜 이렇게 처리 할 수 있을까요? 그래서 나는 모든 학생 이름을 얻을 수 있니? 물건을 infile하기 위해 100 개의 변수를 만들지 않아야합니다.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Student{
private:
int grade;
char grade_letter;
public:
struct Student_info(){
void set_firstname();
void set_lastname();
string get_firstname();
string get_lastname();
};
}myStudent_info;
/// Set/get code below but left out.
int main()
{
Student myStudent[100];
ifstream myfile("input.txt");
if (myfile.is_open())
{
string a, b;
char c;
int d;
myfile >> a >> b >> c >> d;
for (int i = 0; i < 100; i++) {
myStudent[i].myStudentInfo.set_firstname(a);
myStudent[i].myStudentInfo.set_lastname(b);
/// the rest of variables...etc
}
myfile.close();
}
//Exit
cout << endl;
system("pause");
return 0;
}
'myStudent [100]'배열을 만들어 정보를 저장하십시오. – user1336087
'myfile >> a >> b << c << d; '''c '가 나오기 전에''''이라고 가정하고,'d'는 오타라고 가정합니다. – drescherjm
'Student myStudent;는'Student myStudent [100];이어야합니다. – drescherjm