30/09/2018, 21:14

Xin gỡ lỗi của chương trình

Các anh chị cho e hỏi code của e lúc chạy chỉ dk một phần là báo “has stopped working” thì cần phải xử lý như thế nào ạ?
Em xin cảm ơn trước.

#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class sinhvien{
	private:
		int msv;
		string ten;
		float diem;
	public:		
		sinhvien(int m=0, string t=" ", float d=0):msv(m),ten(t),diem(d){
		}
		void nhap();
		void hienthi();
};
void sinhvien::nhap(){
	cout<<"nhap msv"<<endl;
	cin>>msv;
	cout<<"nhap ten"<<endl;fflush(stdin);  
	getline(cin,ten);
	cout<<"nhap diem"<<endl;
	cin>>diem;
fflush(stdin);  
}
void sinhvien::hienthi(){
	cout<<"sinh vien "<<ten<<" co ma sinh vien "<<msv<<" co diem "<<diem<<endl;
}
class hocvien:public sinhvien{
	private:
		string nganh;
		int msv;
		string ten;
		float diem;
	public:
		hocvien(int m=0, string t=" ", float d=0, string n=" "):msv(m),ten(t),diem(d), nganh(n)
		{
		}
		void nhapnganh(){
			sinhvien::nhap();
			cout<<"nhap nganh hoc dao tao:"<<endl;fflush(stdin);  
			getline(cin,nganh);
fflush(stdin);  
		}
		void xuat(){
			sinhvien::hienthi();
			cout<<" nganh hoc dao tao "<<nganh<<endl;
		}
};

int main(){
	int n;
	float t;
	cout<<"nhap so hoc vien: "<<endl;
	cin>>n;
	hocvien A[n];
	for(int i=1;i<=n;i++)
		{cout<<"thong tin hoc vien thu "<<i<<endl;
		A[i].nhapnganh() ;
		}
	for(int i=1;i<=n;i++)
		{A[i].xuat();
		}
	return 0;
}
nohat viết 23:30 ngày 30/09/2018

bạn khai báo mảng như vậy là không có được

hocvien A[n];
Phạm Minh Anh Hữu viết 23:29 ngày 30/09/2018

nếu khai báo mảng như trên thì phải:

hocvien *A=new hocvien[n];
Bài liên quan
0