Lỗi ở hàm void xuất dữ liệu

mọi người cho em hỏi tại sao khi gọi hàm void XuatLopHoc thì chương trình bị lỗi không chạy được ạ
còn nhập nội dung của hàm void XuatLopHoc vào hàm main thì lại chạy được.

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define Max 10000
struct SinhVien
{
    int ma;
    char ten[150];
};
struct LopHoc
{
    int ma;
    char tenlop[150];
    int siso;
    SinhVien dsSV[Max];
};

void NhapSinhVien(SinhVien &sv);
void XuatSinhVien(SinhVien sv);
void NhapLopHoc(LopHoc &lh);
void XuatLopHoc(LopHoc lh);
int main(int argc, char** argv) {
    LopHoc lh1;
    NhapLopHoc(lh1);
    cout<<"Thong tin lop hop:"<<endl;
    //XuatLopHoc(lh1);
    cout<<lh1.ma<<"\t"<<lh1.tenlop<<endl;
    cout<<"Danh sach sinh vien:\n";
    for(int i=0;i<lh1.siso;i++)
    {
        XuatSinhVien(lh1.dsSV[i]);
    }
    return 0;
}
void NhapSinhVien(SinhVien &sv)
{
    cout<<"Nhap ma:";
    cin>>sv.ma;
    cin.ignore();
    cout<<"Nhap ten:";
    gets(sv.ten);
}
void XuatSinhVien(SinhVien sv)
{
    cout<<sv.ma<<"\t"<<sv.ten<<endl;
}
void NhapLopHoc(LopHoc &lh)
{
    cout<<"Nhap ma lop:";
    cin>>lh.ma;
    cin.ignore();
    cout<<"Nhap ten lop:";
    gets(lh.tenlop);
    cout<<"Nhap si so lop:";
    cin>>lh.siso;
    for(int i=0;i<lh.siso;i++)
    {
        cout<<"Nhap sinh vien thu "<<i<<endl;
        NhapSinhVien(lh.dsSV[i]);
    }
}
void XuatLopHoc(LopHoc lh)
{
    cout<<lh.ma<<"\t"<<lh.tenlop<<endl;
    cout<<"Danh sach sinh vien:\n";
    for(int i=0;i<lh.siso;i++)
    {
        XuatSinhVien(lh.dsSV[i]);
    }
}
    LopHoc lh1; // cậu chưa khởi tạo biến này, và biến này không phải pointer
    NhapLopHoc(lh1); // thì làm sao hàm này sửa giá trị của parameter được?
3 Likes

cảm ơn bạn… mình chuyển sang làm con trỏ thì chương trình chạy rồi

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define Max 10000
struct SinhVien
{
  int ma;
  char ten[150];
};
struct LopHoc
{
  int ma;
  char tenlop[150];
  int siso;
  SinhVien **dsSV;
};
SinhVien *NhapSinhVien();
void XuatSinhVien(SinhVien *sv);
void NhapDanhSachSinhVien(SinhVien **&dsSV, int siso);
void XuatDanhSachSinhVien(SinhVien **dsSV, int siso);
LopHoc *NhapLopHoc();
void XuatLopHoc(LopHoc *lh);
void NhapDanhSachLop(LopHoc **&dsLop, int n);
void XuatDanhSachLop(LopHoc **dsLop, int n);
int main(int argc, char** argv) {
  int n;
  cout<<"Nhap so lop hoc:";
  cin>>n;
  LopHoc **dsLop;
  NhapDanhSachLop(dsLop,n);
  cout<<"Danh sach lop da nhap:"<<endl;
  XuatDanhSachLop(dsLop,n);
  return 0;
}
SinhVien *NhapSinhVien()
{
  SinhVien *sv=new SinhVien;
  cout<<"Nhap ma sinh vien:";
  cin>>sv->ma;
  cin.ignore();
  cout<<"Nhap ten sinh vien:";
  gets(sv->ten);
  return sv;
}
void XuatSinhVien(SinhVien *sv)
{
  cout<<sv->ma<<"\t"<<sv->ten<<"\n";
}
void NhapDanhSachSinhVien(SinhVien **&dsSV, int siso)
{
  dsSV=new SinhVien*[siso];
  for(int i=0;i<siso;i++)
  {
      *(dsSV+i)=NhapSinhVien();
  }
}
void XuatDanhSachSinhVien(SinhVien **dsSV, int siso)
{
  for(int i=0;i<siso;i++)
  {
      SinhVien *sv= *(dsSV+i);
      XuatSinhVien(sv);
  }   
}
LopHoc *NhapLopHoc()
{
  LopHoc *lh=new LopHoc;
  cout<<"Nhap ma lop:";
  cin>>lh->ma;
  cin.ignore();
  cout<<"Nhap ten lop:";
  gets(lh->tenlop);
  cout<<"Nhap si so:";
  cin>>lh->siso;
  NhapDanhSachSinhVien(lh->dsSV, lh->siso);
  return lh;
}
void XuatLopHoc(LopHoc *lh)
{
  cout<<"Thong tin lop "<<lh->ma<<" "<<lh->tenlop<<":\n";
  XuatDanhSachSinhVien(lh->dsSV,lh->siso);
}
void NhapDanhSachLop(LopHoc **&dsLop, int n)
{
  dsLop=new LopHoc*[n];
  for(int i=0;i<n;i++)
  {
      *(dsLop+i)=NhapLopHoc();
  }
}
> 
void XuatDanhSachLop(LopHoc **dsLop, int n)
{
  for(int i=0;i<n;i++)
  {
      LopHoc *lh= *(dsLop+i);
      XuatLopHoc(lh);
  }   
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?