mình đã code bài này khi chạy thì phần nhập điểm nó bị dính vào nhau luôn không nhập được dù đã sử dụng câu lệnh cin.ignore();
#include<iostream>
#include<fstream>
#include<cstdio>
using namespace std;
class hocsinh{
string hoten;
string lop;
float dt,dl,dh;
public:
string getten(){
return hoten;
}
string getlop(){
return lop;
}
float getdt(){
return dt;
}
float getdl(){
return dl;
}
float getdh(){
return dh;
}
void nhap(){
cout<<"nhap vao ten:";
getline(cin,hoten);
cin.ignore();
cout<<"nhap vao lop:";
getline(cin,lop);
cin.ignore();
cout<<"nhap vao diem toan,li,hoa:";
cin>>dt;
cin>>dl;
cin>>dh;
}
void xuat(){
cout<<"ten:"<<hoten<<endl;
cout<<"lop:"<<lop<<endl;
cout<<"diem:"<<dt<<" "<<dl<<" "<<dh<<endl;
}
float diemtb(){
return (dt+dl+dh)/3;
}
};
main(){
int n;
cout<<"nhap vao so hoc sinh:";cin>>n;
hocsinh hs[n];
for(int i=0;i<n;i++){
hs[i].nhap();
}
for(int i=0;i<n;i++){
hs[i].xuat();
}
char tenf[100];
gets(tenf);
ofstream f(tenf,ios::out);
while(!f.fail()){
cout<<"file loi hoac khong ton tai,moi ban nhap lai ten file:"<<endl;
gets(tenf);
}
for(int i=0;i<n;i++){
f<<hs[i].getten()<<hs[i].getlop()<<hs[i].getdt()<<hs[i].getdl()<<hs[i].getdh()<<hs[i].diemtb();
}
}
Huy nói quá chhuẩn. bạn bị sao nói rõ ra đi thử debugging xem
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?