Không thêm được số lượng sinh viên vào trong danh sách ban đầu bằng cấu trúc struct

khi nhập sinh viên vào thì được nhưng xuất ra danh sách sinh viên chỉ xuất ra danh sách ban đầu
và em không hiểu tại sao khi truyền tham chiếu vào themmotsinhvien(int soluong,danhsach &ds) thì báo lỗi
đây là code của em

#include<iostream>
#include<string.h>
using namespace std;
typedef struct sinhvien
{
	string ten;
	float diem;
	int mssv;
};
void nhapsinhvien(sinhvien &x)
{
	fflush(stdin);
	cout<<"\nnhap ten sinh vien:";
	getline(cin,x.ten);
	fflush(stdin);
	cout<<"\nnhap diem:";cin>>x.diem;
	fflush(stdin);
	cout<<"\nnhap ma sinh vien:";cin>>x.mssv;
}
void xuatmotsinhvien(sinhvien x)
{
	cout<<"\nten:"<<x.ten;
	cout<<"\ndiem:"<<x.diem;
	cout<<"\nmssv:"<<x.mssv;
}
typedef struct danhsach
{
	int n;
	sinhvien *arr;
};
void nhapnhieusinhvien(danhsach ds)
{
	int i;
	for(i=0;i<ds.n;i++)
	{
		nhapsinhvien(ds.arr[i]);
	}
}
void xuatnhieusinhvien(danhsach ds)
{
	
	int i;
	for(i=0;i<ds.n;i++)
	{
		xuatmotsinhvien(ds.arr[i]);
	}
}
void themmotsinhvien(int soluong,danhsach ds)
{
	sinhvien t;
	int i,z;
	z=(ds.n)+soluong;
	for(i=ds.n;i<z;i++)
	{
		ds.arr=new sinhvien[(ds.n)++];
		nhapsinhvien(t);
		
	}
		
}
int main()
{
	int soluong;
	sinhvien x;
	danhsach ds;
	cout<<"nhap so luong sinh vien:";cin>>ds.n;
	ds.arr=new sinhvien[ds.n];
	nhapnhieusinhvien(ds);
	cout<<"\nmang sau khi them sinh vien:";
	cout<<"\nnhap so luong sinh vien can them:";cin>>soluong;
	themmotsinhvien(soluong,ds);
	xuatnhieusinhvien(ds);
}

Update một chút,

  • Bỏ hết flush(), hàm này vô dụng với stream.
  • Chỗ ??? tự xử nhé
#include<iostream>
#include<string>

using namespace std;

typedef struct sinhvien
{
	string ten;
	float diem;
	int mssv;
};

void nhapsinhvien(sinhvien &x)
{
	cout << "\nnhap ten sinh vien:";
	getline(cin, x.ten);
	
	cout << "\nnhap diem:"; cin >> x.diem;
	cin.ignore();
	cout << "\nnhap ma sinh vien:"; cin >> x.mssv;
	cin.ignore();
}

void xuatmotsinhvien(sinhvien x)
{
	cout << "\nten:" << x.ten;
	cout << "\ndiem:" << x.diem;
	cout << "\nmssv:" << x.mssv;
}

typedef struct danhsach
{
	int n;
	sinhvien *arr;
};
void nhapnhieusinhvien(danhsach ds)
{
	int i;
	for (i = 0; i<ds.n; i++)
	{
		nhapsinhvien(ds.arr[i]);
	}
}
void xuatnhieusinhvien(danhsach ds)
{

	int i;
	for (i = 0; i<ds.n; i++)
	{
		xuatmotsinhvien(ds.arr[i]);
	}
}
void themmotsinhvien(int soluong, danhsach ds)
{
	sinhvien t;
	int i, z;
	z = (ds.n) + soluong;
	for (i = ds.n; i<z; i++)
	{
		ds.arr = new sinhvien[(ds.n)++]; // chỗ này tại sao reset hết dữ liệu???
		nhapsinhvien(t);

	}

}
int main()
{
	int soluong;
	char stopKey;
	sinhvien x;
	danhsach ds;
	cout << "nhap so luong sinh vien:"; 
	cin >> ds.n; cin.ignore();
	ds.arr = new sinhvien[ds.n];

	nhapnhieusinhvien(ds);
	cout << "\nmang sau khi them sinh vien:";
	cout << "\nnhap so luong sinh vien can them:"; cin >> soluong; cin.ignore();
	themmotsinhvien(soluong, ds);
	xuatnhieusinhvien(ds);

	cin >> stopKey;
	return 0;
}
1 Like

sao em thử chạy code của anh sao ko được vậy

VS Community 2015…

1 Like

chỗ hàm themmotsinhvien bạn khởi tạo struct trong vòng lặp for -> nó bị lỗi là đúng rồi!. Bạn để nó ra ngoài đi.

cám ơn hai anh nhiều em cuối cùng cũng làm ra rồi anh có thể cho em xin facebook đề làm quen không
đây là code của em

#include<iostream>
#include<string.h>
using namespace std;
typedef struct sinhvien
{
	string ten;
	float diem;
	int mssv;
};
void nhapsinhvien(sinhvien &x)
{
	fflush(stdin);
	cout<<"\nnhap ten sinh vien:";
	getline(cin,x.ten);
	fflush(stdin);
	cout<<"\nnhap diem:";cin>>x.diem;
	fflush(stdin);
	cout<<"\nnhap ma sinh vien:";cin>>x.mssv;
}
void xuatmotsinhvien(sinhvien x)
{
	cout<<"\nten:"<<x.ten;
	cout<<"\ndiem:"<<x.diem;
	cout<<"\nmssv:"<<x.mssv;
}
typedef struct danhsach
{
	int n;
	sinhvien *arr;
};
void nhapnhieusinhvien(danhsach ds)
{
	int i;
	for(i=0;i<ds.n;i++)
	{
		nhapsinhvien(ds.arr[i]);
	}
}
void xuatnhieusinhvien(danhsach ds)
{
	
	int i;
	for(i=0;i<ds.n;i++)
	{
		xuatmotsinhvien(ds.arr[i]);
	}
}
void themmotsinhvien(int soluong,danhsach &ds)
{
	int i,z,k;
	z=(ds.n)+soluong;
	k=ds.n;
	// do tung sinh vien sang mot mang moi 
	 sinhvien *brr= new sinhvien[ds.n];
	 for(i=0;i<ds.n;i++)
	 {
	 	brr[i]=ds.arr[i];
	 }
	 // cap phat co vung nho cua mang a du bang voi sl input
	 ds.arr=new sinhvien[(ds.n)+soluong];
	 // do mang b sang lai mang dau
	 for(i=0;i<ds.n;i++)
	 {
	 	ds.arr[i]=brr[i];
	 }
	 for(i=k;i<z;i++)
	 {
	 	nhapsinhvien(ds.arr[i]);
	 	ds.n++;
	 }
		
}
int main()
{
	int soluong;
	sinhvien x;
	danhsach ds;
	cout<<"nhap so luong sinh vien:";cin>>ds.n;
	ds.arr=new sinhvien[ds.n];
	nhapnhieusinhvien(ds);
	cout<<"\nmang sau khi them sinh vien:";
	cout<<"\nnhap so luong sinh vien can them:";cin>>soluong;
	themmotsinhvien(soluong,ds);
	xuatnhieusinhvien(ds);
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?