代码在输入名称的第一个循环后不起作用,但是获得了rollno的输入和输出。 我认为cin.getline部分存在问题。任何帮助将不胜感激。
#include<iostream>
#include<fstream>
using namespace std;
class lmao {
char name[50];
char rollno;
public:
void display(void);
void input(void);
};
void lmao::input(){
cout<<"Enter name: \n";
cin.getline(name,50);
cout<<"Enter roll no.: \n";
cin>>rollno;
cout<<"\n";
}
void lmao::display(){
cout<<"The name is: "<<name<<"\n";
cout<<"The roll no. is: "<<rollno<<"\n";
}
int main(){
lmao lol[3];
ofstream fout;
fout.open("cuz.txt",ios::in);
for(int i=0;i<3;i++){
lol[i].input();
fout.write((char*)&lol[i],sizeof(lol[i]));
}
fout.seekp(0);
fout.close();
cout<<"Contents are: \n";
ifstream fin;
fin.open("cuz.txt", ios::in);
for(int i=0;i<3;i++){
fin.read((char*)&lol[i],sizeof(lol[i]));
lol[i].display();
}
fin.close();
return 0;
}