好吧,我想通了,今天學到了很多東西,我想爲此感謝社區。我現在幾個小時都沒有在路上碰到任何東西,但現在我被卡住了。保存並加載C++程序
路上的最後一個碰撞。保存並加載我的程序。我不知道從哪裏開始。我研究了fwrite ......和fread ......是如何工作的,而所有的例子都是針對沒有拆分的程序。我不知道從哪裏開始我的文件。我會提出2個功能。如果有人可以幫助我如何保存那些,我可能會找出其餘的。
在gradebook.h
class Student {
public:
string last;
string first;
int student_id;
};
class Course {
public:
string name;
int course_id;
vector <Student> students;
};
class Gradebook {
public:
Gradebook();
void addCourse();
void addStudent();
private:
vector <Course> courses;
};
在gradebook.cpp
void Gradebook::addCourse() {
int i, loop=0;
cout << "Enter Number of Courses: ";
cin >> loop;
for(i=0; i<loop; i++) {
//create newEntry to store variables
Course newEntry;
cout << "Enter Course ID: ";
cin >> newEntry.course_id;
cout << "Enter Course Name: ";
cin >> newEntry.name;
//set variables from newEntry in Courses
courses.push_back(newEntry);
}
}
void Gradebook::addStudent() {
int i, loop=0;
cout << "Enter Number of Students: ";
cin >> loop;
for(i=0; i<loop; i++) {
//create newEntry to store variables
Student newEntry;
cout << "Enter Student ID: ";
cin >> newEntry.student_id;
cout << "Enter Last Name: ";
cin >> newEntry.last;
cout << "Enter First Name: ";
cin >> newEntry.first;
//set variables from newEntry in Students
courses[0].students.push_back(newEntry);
}
}
因此,如果用戶是輸入課程和學生的一些變量如何將我使用的fwrite ...保存數據?
ofstream怎麼知道需要什麼信息出去? – user2816227
你告訴它!我不知道你想輸出什麼。用您想要輸出的內容替換'thedata'。像'cout'一樣工作 – yizzlez
您必須重載全球運營商<< – dsi