Code chạy ra output có nhiều những kí tự lạ

Em bị lỗi như thế này là sao ạ!?

Đây là code của em: http://codepad.org/YvCs6NZw

#include<iostream>
#include<string.h>

using namespace std;
struct score{
	float diemToan;
	float diemLy;
	float diemHoa;
	float tongDiem;
};
struct thiSinh{
	
	string 	so_bao_danh;
	string 	ho_ten;
	string 	dia_chi;
	string 	gioi_tinh;
	score	diem;
	int 	phong_thi;
};

//void inPut(thiSinh *a);
//void outPut(thiSinh *a);
//void sumScore(thiSinh *a);
//void menu(thiSinh *ds[], int n);
//void menuPhu(thiSinh *ds[],thiSinh *a,int n);

void inPut(thiSinh *a){
	cout<<"\nNhap ten thi sinh: ";
	fflush(stdin);
	getline(cin,(a->ho_ten));
	cout<<"\nNhap gioi tinh: ";
	fflush(stdin);
	getline(cin,(a->gioi_tinh));
	cout<<"\nNhap so bao danh: ";
	fflush(stdin);
	getline(cin,(a->so_bao_danh));
	cout<<"\nNhap ho khau (xa/huyen/tinh): ";
	fflush(stdin);
	getline(cin,(a->dia_chi));
	cout<<"Nhap diem toan: ";
	cin>>(a->diem.diemToan);
	cout<<"Nhap diem ly: ";
	cin>>(a->diem.diemLy);
	cout<<"Nhap diem hoa: ";
	cin>>(a->diem.diemHoa);
	cout<<"Nhap so phong: ";
	cin>>(a->phong_thi);
}
void sumScore(thiSinh *a){
	a->diem.tongDiem = (a->diem.diemHoa)+(a->diem.diemLy)+(a->diem.diemToan);
}
void outPut(thiSinh *a){
	cout<<a->so_bao_danh<<", ";
	cout<<a->ho_ten<<", ";
	cout<<a->gioi_tinh<<", ";
	cout<<"Phong thi: "<<a->phong_thi<<", ";
	cout<<"Diem toan: "<<a->diem.diemToan<<", ";
	cout<<"Diem ly: "<<a->diem.diemLy<<", ";
	cout<<"Diem hoa: "<<a->diem.diemHoa<<", ";
	sumScore(a);
	cout<<"Diem tong: "<<a->diem.tongDiem<<".";	
}
void xapxepTang(thiSinh *ds[], int n){
	for(int i = 0; i < n-1; i++){
		for(int j = 0; j < n; i++){
			if(ds[i]->diem.tongDiem > ds[j]->diem.tongDiem){
				float temp = ds[i]->diem.tongDiem;
				ds[i]->diem.tongDiem = ds[j]->diem.tongDiem;
				ds[j]->diem.tongDiem = temp;
			}
		}
	}
}
void menuPhu(thiSinh *ds[], int n){
	int selec1;
	system("cls");
	cout<<"1.Xuat thong tin theo danh sach da nhap.\n";
	cout<<"2.Xuat thong tin theo danh sach phong.\n";
	cout<<"3.Xuat thong tin theo thu tu tong diem tang dan.\n";
	cout<<"4.Xuat thong tin theo thu tu tong diem giam dan.\n";	
	cout<<"5.Xuat thong tin theo thu tu tong diem ly tang dan.\n";
	cout<<"6.Xuat thong tin theo thu tu tong diem ly giam dan.\n";		
	cout<<"7.Xuat thong tin theo thu tu tong diem hoa tang dan.\n";
	cout<<"9.Xuat thong tin theo thu tu tong diem hoa giam dan.\n";
	cout<<"10.Xuat thong tin theo thu tu tong diem toan tang dan.\n";
	cout<<"11.Xuat thong tin theo thu tu tong diem toan giam dan.\n";
	cout<<"\n=================================================\n";
	
	cout<<"\nNhap lua chon: ";
	cin>>selec1;
	
	if(selec1 == 1){
		for(int i = 0; i < n; i++){
			outPut(ds[n]);
			system("pause");
		}
	}
	else
		if(selec1 == 2){
			xapxepTang(ds,n);
			for(int i = 0; i < n; i++){
			outPut(ds[n]);
			system("pause");
			}
		}
}
void menu(thiSinh *ds[], int n){
	
	while(true){
		system("cls");
		cout<<"\n===============================================\n";
		cout<<"1.Nhap thong tin thi sinh.\n";
		cout<<"2.Xuat thong tin thi sinh.\n";
		cout<<"0.Ket thuc.\n";
		cout<<"\n===============================================\n";
		int selec;
		cout<<"Nhap lua chon:\n";
		cin>>selec;
		if(selec == 0){
			break;
		}
		else
			if(selec == 1){
				thiSinh *a = new thiSinh;
				inPut(a);
				ds[n] = a;
				n++;
				delete a;
			}
			else
				if(selec == 2){
						menuPhu(ds,n);
						system("pause");
					}				
	}
}


int main(){
	thiSinh *ds[200];
	thiSinh *a = new thiSinh;
	int n = 0;
	menu(ds,n);
	
}

Em cảm ơn!

Tạo mới và xóa luôn, chưa in ấn gì.

3 Likes

Em để cái delete ở cuối hàm đó rồi mà code vẫn bị như vậy @@

Line 124 - 128 là tạo ra 1 thí sinh mới a , cho vào mảng ds rồi xóa luôn a. Nhưng vì ở đây a là pointer nên sau khi xóa a thì cũng sẽ xóa luôn cái vừa add vào mảng ds , do đó sẽ bị lỗi.

Để sửa thì có cách đơn giản thế này : ds chỉ cần là 1 mảng chứa các thí sinh ( biến struct bình thường), không cần là 1 mảng chứa các con trỏ.
Line 140 : thiSinh *ds[200] là 1 mảng có 200 phần tử, mỗi phần tử là 1 con trỏ trỏ đến 1 thí sinh
Nên sửa nó thành đơn giản là :

thiSinh ds[200]; 

Xóa line 141 đi vì không để làm gì cả.

Để Line 124 - 128 thành

thiSinh a;
inPut(&a);
ds[n] = a;
n++;

tương ứng các hàm như menu(), menuphu() mình chỉ cần truyền vào là array ds, nên sửa nó thành

void menu(thiSinh* ds, int n) 
void menuPhu(thiSinh* ds, int n)
6 Likes

Hiểu rất ngược ý của mình, dù sao thì @MBTRevolution đã giải thích và chỉ cho bạn rồi.

3 Likes

Như vậy em hiểu rồi anh!! Cảm ơn anh nhiều ạ!!

Lần sau em sẽ suy nghĩ lại kĩ hơn!

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