Lỗi đọc file khó hiểu trong C++, Close the program?

Mình làm bài này và đã làm xong nhưng lúc chạy nhâp xong hế thì ra như thế này, mọi người xem sai chỗ nào được không ạ.

#include<string>
#include<fstream>
using namespace std;
class hang{
	protected:
		string tenhang;
		int dongia;
	public:
		void set_tenhang(string x){
			tenhang=x;
		}
		void set_dongia(int x){
		    dongia=x;
		}
		int get_dongia(){
			return dongia;
		}
		string get_tenhang(){
			return tenhang;
		}
		void nhap(){
			cout<<"nhap ten hang : "; getline(cin,tenhang);
			cout<<"nhap don gia : "; cin>>dongia; cin.ignore();
		}
		void xuat(){
			cout<<"ten hang : "<<tenhang<<endl;
			cout<<"don gia : "<<dongia<<endl;
		}
};
class banggia{
	private:
		hang a[100];
		int tshang;
	public:
		int get_tshang(){
			return tshang;
		}
		void nhapbp(){
			cout<<"nhap ts hang: "; cin>>tshang; cin.ignore();
			for(int i=0;i<tshang;i++){
				a[i].nhap();
			}
		}
		void xemmh(){
			cout<<"THONG TIN BANG GIA"<<endl;
			for(int i=0;i<tshang;i++){
				a[i].xuat();
			}
		}
		int xemgia(string x){
		    for(int i=0;i<tshang;i++){
		    	if(a[i].get_tenhang()==x) return a[i].get_dongia();
			}
		}
		hang* get_a(){
		return a;}
};
class hangban: public hang{
	private:
		int soluong;
		int thanhtien;
	public:
		void set_soluong(int x){
			soluong=x;
		}
		void set_thanhtien(int x){
			thanhtien=x;
		}
		int get_soluong(){
			return soluong;
		}
		int get_thanhtien(){
			return thanhtien;
		}
		void tinhtien(){
			thanhtien= get_dongia()*soluong;
		}
		
};
class hoadon{
	private:
		int sohoadon;
		int tongtien;
		int tshangban;
		hangban b[100];
	public:
		void nhapbp(banggia g){
			tongtien=0;
			cout<<"NHAP THONG TIN HOA DON\n";
			cout<<"nhap tong so hang ban: " ;cin>>tshangban;cin.ignore();
			for(int i=0;i<tshangban;i++){ 
				string tenhangIN; int soluongIN; 
				cout<<"nhap ten hang: "; getline(cin,tenhangIN);
				b[i].set_tenhang(tenhangIN);
				cout<<"nhap so luong: "; cin>>soluongIN; cin.ignore();
				b[i].set_dongia(g.xemgia(b[i].get_tenhang()));
				b[i].set_soluong(soluongIN);
				b[i].set_thanhtien(b[i].get_dongia()*b[i].get_soluong());
				tongtien+=b[i].get_thanhtien();
			}
		}
		void xemmh(){
			cout<<"TTHONG TIN HOA DON "<<endl; 
			for(int i=0;i<tshangban;i++){ 
			    b[i].xuat();
				cout<<"so luong: "<<b[i].get_soluong()<<endl;
				cout<<"Tien: "<<b[i].get_thanhtien()<<endl;
			}
			cout<<"TONG TIEN: "<<tongtien<<endl;
		}
		void docfile(){
	    	fstream f("hoadon.dat",ios::in|ios::binary);
	    	f.read(reinterpret_cast <char*> (this),sizeof(hoadon));
	    	f.close();
		}
		void ghifile(){
	    	fstream f("hoadon.dat",ios::out|ios::binary);
	    	f.write(reinterpret_cast <char*> (this),sizeof(hoadon));
	    	f.close();
		}	
};
main(){
	banggia a;	a.nhapbp(); a.xemmh();
	hoadon b; b.nhapbp(a); b.xemmh();
	b.ghifile();
	hoadon m; 
    m.docfile(); m.xemmh();
}

xài string thì ko ghi xuống binary file bằng 1 dòng được

4 Likes

vậy giờ mình phải ghi như thế nào nhỉ

thay string bằng mảng char, sửa lại các hàm get/set tương ứng, sửa luôn mấy cái so sánh == bằng strcmp…

4 Likes

ok bạn, cảm ơn bạn nhiều nha

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