创建动态对象的问题

时间:2014-09-05 10:27:00

标签: c++ class file-io scope dynamic-memory-allocation

我正在创建一个需要创建动态类对象的项目。

void representative(){
int x;
pharmacy_personnel ob1;
A:
clrscr();
cout<<"Please select what you would like to do:"<<endl
    <<"1. Add Medicines to the pharmacy"<<endl
    <<"Enter your Choice >> ";cin>>x;

switch(x){
    case 1: {
        char y;
        fstream file(Fname,ios::in|ios::binary);
        pharmacy_personnel temp;
        while(file.read((char*)&temp,sizeof(temp)))
            total++;    

        A:
        clrscr();
        cout<<"Please fill in the fields required to add the medicine to the records"<<endl;
            temp.add_medicine(Fname);
            total++;// this is to get the total no. of records on the file
        *pharmacy_personnel *ob1;
        ob1=new pharmacy_personnel [total];

        for(int i=0; i<total; i++)
            file.read((char*)&ob1[i],sizeof(ob1));*

        B:
        cout<<"Do you want to add another medicine?(y/n): "; cin>>y;
            if(y=='y'||y=='Y')
                goto A;
            else
            if(y=='n'||y=='N')
                break;
            else
            {
            cout<<"Please Enter a valid response"<<endl;
            clrscr();
            goto B;
            }
        pharmacy_personnel *ob1;
        ob1=new pharmacy_personnel [total];

        } break;

        default: cout<<"Please choose from 1->5 only"<<endl; getch();
        goto A;

}
        delete ob1;   }

总计是全局变量

我想知道是否有更好的方法来完成整个事情,因为总价值会经常变化 我如何在另一个UDF中全局访问这些对象?

0 个答案:

没有答案