C++ 仅在交替步骤中写入文件的数据

C++ 仅在交替步骤中写入文件的数据,c++,visual-c++,C++,Visual C++,代码 #包括 #包括 #包括 #包括 使用名称空间std; 班级学生 { int admno; 字符名[20]; //字符地址[20]; //字符串名; 公众: void getdata() { coutname; 库塔德诺; //getch(); } void showdata() { cout您应该将开关放入一个循环中: #include<iostream> #include<fstream> #include<conio.h> #include<s

代码

#包括
#包括
#包括
#包括
使用名称空间std;
班级学生
{
int admno;
字符名[20];
//字符地址[20];
//字符串名;
公众:
void getdata()
{
coutname;
库塔德诺;
//getch();
}
void showdata()
{

cout您应该将
开关
放入一个循环中:

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

using namespace std;
class student
{
            int admno;
           char name[20];
          // char address[20];
           //string name;
public:
          void getdata()
          {
                     cout<<"\n\nEnter The Name of The Student ";
                     //gets(name);
                     //cin.ignore();
                     cin>>name;

                     cout<<"\nEnter The admission no. ";
                     cin>>admno;


                     // getch();
          }
          void showdata()
          {
                     cout<<"\nAdmission no. : "<<admno;
                     cout<<"\nStudent Name : "<<name<<endl;

                     //puts(name);

          }


          void display()
      {
          //student obj;
          ifstream fp1;
          fp1.open("student.dat",ios::binary);
          while(fp1.read((char*)this,sizeof(*this)))
          {
                     this->showdata();
                     fp1.read((char*)this,sizeof(*this));
          }

          fp1.close();
      }

          void add()
          {
          ofstream fp2;
          fp2.open("student.dat",ios::binary|ios::app);
          this->getdata();
          fp2.write((char*)this,sizeof(*this));
           fp2.close();
          }



};


int main()
{
     student obj;
    //system("cls");
    cout<<"\n1. Add new student";
    cout<<"\n2. View all student";
    cout<<"\n3. Search student";
    cout<<"\n4. modify student";
    cout<<"\n5. delete student";
    cout<<"\n6. Exit";
    cout<<"\n\nEnter your choice";
    int ch;
    cin>>ch;
     switch(ch)
           {
                     case 1:

                            obj.add();
                            break;                        
                     case 2:
                           obj.display();
                            //b.viewbook();
                            break;

                     default:
                            cout<<"Enter Valid choice";
           }





return 0;
}
bool exitLoop=false;
而(!exitLoop){
int-ch;
cin>>ch;
开关(ch){
案例“1”:
obj.add();
打破
案例“2”:
obj.display();
//b、 viewbook();
打破
案例“6”:
exitLoop=true;
打破
违约:
库特
bool exitLoop = false;
while(!exitLoop) {
     int ch;
     cin>>ch;
     switch(ch) {
         case '1':
             obj.add();
             break;                        
         case '2':
             obj.display();
             //b.viewbook();
             break;
         case '6':
             exitLoop = true;
             break;
         default:
             cout<<"Enter Valid choice";
     }
}