Lưu sinh viên, giảng viên vừa nhập vào trong file nhưng trong file chỉ nhận được phần tử cuối cùng

Đề bài: Vừa nhập sinh viên, vừa nhập giảng viên và lưu vào trong file

#include<fstream>
#include<iostream>
using namespace std;
class nguoi{
	public:
		string name;
		int id;
		virtual void output(){
			ofstream fo;
			fo.open("D:\\dai.txt");
			if(fo.is_open())
			{
				cout<<"nhap ID:";
				cin>>id;
				cout<<"nhap ten:";
				cin>>name;
				fo<<id<<"."<<name<<endl;	
				
				
//				fo.close();
//				cout<<"da ghi vao file"<<endl;
//				cout<<"=========================="<<endl;
			}
			else 
				cout<<"file error//////";
		}
		virtual void input(){

			ifstream fi;
			string line;
			fi.open("D:\\dai.txt");
			if(fi.is_open())
			{
				while(!fi.eof())
				{
					getline(fi,line);
					cout<<line<<endl;
				
				}
			//	fi.close();
			}
			else
				cout<<"file error";
		}
		virtual float tinhthuong(){
		}
};
class giangvien:public nguoi{
	public:
		int sobaibao;
		void output(){
			//nguoi::output();
			ofstream fo;
			fo.open("D:\\dai.txt");
			if(fo.is_open())
			{
				nguoi::output();
				cout<<"nhap so bai bao:";
				cin>>sobaibao;
				fo<<sobaibao<<endl;	
				
				
//				fo.close();
//				cout<<"da ghi vao file"<<endl;
			}
			else 
				cout<<"file error//////";
		}
		void input(){
			nguoi::input();
			ifstream fi;
			string line;
			fi.open("D:\\dai.txt");
			if(fi.is_open())
			{
				while(!fi.eof())
				{
					getline(fi,line);
					cout<<line<<endl;
				}
			//	fi.close();
			}
			else
				cout<<"file error";
		}
		float tinhthuog(){
			if(sobaibao>=3)
				return sobaibao*1000000;
			else
				return 0;
		}
		
};
class sinhvien:public nguoi{
	public:
		float dtb;
		void output(){
			//nguoi::output();
			ofstream fo;
			fo.open("D:\\dai.txt");
			if(fo.is_open())
			{
				nguoi::output();
				cout<<"nhap diem trung binh:";
				cin>>dtb;
				fo<<dtb<<endl;	
				
				
//				fo.close();
//				cout<<"da ghi vao file"<<endl;
			}
			else 
				cout<<"file error//////";
		}
		void input(){
			
			ifstream fi;
			string line;
			fi.open("D:\\dai.txt");
			if(fi.is_open())
			{
				nguoi::input();
				while(!fi.eof())
				{
					getline(fi,line);
					cout<<line<<endl;
				}
			//	fi.close();
			}
			else
				cout<<"file error";
		}
		float tinhthuog(){
			if(dtb>=3)
				return dtb*300000;
			else
				return 0;
		}
		
};
class nguoilist{
	public:
		int k;
		int chon;
		nguoi *n[10];
		void outputlist()
		{
			ofstream fo;
			fo.open("D:\\dai.txt");
			if(fo.is_open())
			{
				k=0;
				while(1)
				{
					cout<<"chon: 1-giang vien!"<<endl;
					cout<<"chon: 2-sinh vien!"<<endl;
					cout<<"chon: 3-Thoat!"<<endl;
						cin>>chon;
					if (chon==3)
						break;
					if (chon==1)
							n[k]=new giangvien();
					if (chon==2)
					n[k]=new sinhvien();
					n[k]->output();	
					k++;					
				}	
				fo.close();
			}
			else
				cout<<"file error";
		}
		void inputlist(){
			
			string line;
			ifstream fi;
			fi.open("D:\\dai.txt");
			if(fi.is_open())
			{
				while(!fi.eof())
				{
					getline(fi,line);
					cout<<line<<endl;
				
				}
				fi.close();
			}
			else
				cout<<"file error";
			
		}
};
int main()
{
	nguoilist ng;
	ng.outputlist();
	cout<<"dang doc file   (^_^)  "<<endl;
	ng.inputlist();
	return 0;
}

mình ko ghi file ra đc , file chỉ nhân phần tử cuối cùng

Nội dung #2 - #4 đã được cập nhật vào #1 rồi nên mình move sang off-topic :kissing:

Vì bạn mở tập tin dạng ghi đè, mọi dữ liệu trước đó đều bị loại bỏ.
ofstream.open() còn có tham số thứ 2 nữa. Tham khảo:
http://www.cplusplus.com/reference/fstream/ofstream/open/

Ngoài ra, bạn phá luôn hướng đối tượng.

3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?