Hỏi về cách thêm môn học trong phiếu điểm

Moị người giúp em làm sao để thêm một môn học trong phiếu và hiển thị với ạ. Em cảm ơn.

#include<bits/stdc++.h>
using namespace std;

class PHIEUBAODIEM;
class BANGDIEM {
	char tenmon[30];
	int sotrinh;
	float diem;
	public:
		void nhap();
		void xuat();
		friend class PHIEUBAODIEM;
		friend void xuattenmon_sotrinh(PHIEUBAODIEM a);
		friend void themmonhoc(PHIEUBAODIEM a);
};
void BANGDIEM::nhap() {
	cout << " ten mon: "; fflush(stdin); gets(tenmon);
	cout << " so trinh: "; cin >> sotrinh;
	cout << " diem: "; cin >> diem;
}
void BANGDIEM::xuat() {
	cout << setw(30) << left << tenmon
		<< setw(20) << left << sotrinh
		<< setw(20) << left << diem << endl;
}
class PHIEUBAODIEM {
	char masv[10];
	char tensv[30];
	char lop[10];
	int khoa;
	BANGDIEM *x;
	int n;
	float diemtb;
	public:
		void nhap();
		void xuat();
		friend void xuattenmon_sotrinh(PHIEUBAODIEM a);
		friend void themmonhoc(PHIEUBAODIEM a);
};
void PHIEUBAODIEM::nhap() {
	cout << "ma sv: "; fflush(stdin); gets(masv);
	cout << "ten sv: "; gets(tensv);
	cout << "lop: "; gets(lop);
	cout << "khoa: "; cin >> khoa;
	cout << "so mon hoc: "; cin >> n;
	x = new BANGDIEM[n];
	diemtb = 0;
	float tongdiem = 0;
	int tongsotrinh = 0;
	for(int i = 0; i < n; i++) {
		cout << "mon thu " << i+1 << ": \n";
		x[i].nhap();
		tongdiem += x[i].diem * x[i].sotrinh;
		tongsotrinh += x[i].sotrinh;
	}
	diemtb = tongdiem / tongsotrinh;
}
void tieude() {
	cout << setw(30) << left << "\nTEN MON"
		<< setw(20) << left << "SO TRINH"
		<< setw(20) << left << "DIEM" << endl;
}
void PHIEUBAODIEM::xuat() {
	cout << "ma sv: " << masv << endl;
	cout << "ten sv: " << tensv << endl;
	cout << "lop: " << lop << endl;
	cout << "khoa: " << khoa << endl;
	if(n == 0) {
		cout << "so mon hoc: 0" << endl;
	}
	else {
		cout << "so mon hoc: " << n << endl;
		tieude();
		for(int i = 0; i < n; i++) {
			x[i].xuat();
		}
		cout << "\ndiem tb: " << diemtb << endl;
	}
}
void xuattenmon_sotrinh(PHIEUBAODIEM a) {
	if(a.n > 0) {
		cout << "ten mon hoc co so trinh > 3: ";
		for(int i = 0; i < a.n; i++) {
			if(a.x[i].sotrinh > 3) {
				cout << a.x[i].tenmon << ", ";
			}
		}
		cout << endl;
	}
	else {
		cout << "khong co ten mon hoc co so trinh > 3.\n";
	}
}
void themmonhoc(PHIEUBAODIEM a) {
	
}
int main()
{
	PHIEUBAODIEM a;
	cout << "\n\t\tNHAP THONG TIN PHIEU BAO DIEM\n\n";
	a.nhap();
	cout << "\n\t\tXUAT THONG TIN PHIEU BAO DIEM\n\n";
	a.xuat();
	
	cout << "\n BONUS 1:\n";
	xuattenmon_sotrinh(a);
	
	cout << "\n BONUS 2:\n";
	themmonhoc(a);
	cout << "\n\t\tXUAT THONG TIN PHIEU BAO DIEM\n\n";
	a.xuat();
	return 0;
}

Hi there,
Tớ không thấy cài đặt của themmonhoc (ít nhất phần lấy dữ liệu cậu nên cài đặt được).
Cậu gặp vấn đề gì khi cài đặt nó vậy?

3 Likes

mình làm như này nhưng nó không chạy được nên mình xóa

void themmonhoc(PHIEUBAODIEM &a) {
	a.n++;
	cout << "Nhap mon hoc can them";
	a.x[a.n-1].nhap();
}

EDIT (@library): @Canh_Tran Cậu nhớ dùng Markdown để format code từ các post sau nhé! :smile:

“Không chạy được” là như thế nào bạn?
Bị “trôi” là do fflush(stdin) không có tác dụng gì cả (nó sai ngay từ khái niệm :smiley:) , cin.ignore mới được.

5 Likes

cảm ơn mọi người, bài mình chạy được rồi

Close the topic since the owner already solved the problem by himself. Nice job!

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