从File读取值到类变量?

时间:2017-09-13 01:22:27

标签: c++

我试图编写一个代码来获取文件中的值并将它们存储到类变量中。我根据我的代码不保存它们。我真的很感激帮助。我不知道我的错误在哪里。文件中的第一个值是总共有的类对象数。

class customer {
    private:
        string *names;
        char *plans;
        int *hours;
        int customerTotal;
    public:
       string name;
       char plan;
       int hour;

       customer() {
              name = "";
              plan = ' ';
              hour = 0;
              customerTotal = 0;
        }
       void setName(string x) {
              names[customerTotal] = x;
        }
       void setPlan(char x) {
              plans[customerTotal] = x;
        }
       void setHours(int x){
              hours[customerTotal] = x;
              customerTotal++;
        }
       void printArray(){
             for (int i = 0; i < customerTotal;i++){
           cout << names[i] << plans[i] << hours[i] << endl;
         }

    }
};

int main() {
    int numCustomers;
    ifstream inFile;
    string n;
    char p;
    int h;


    inFile.open("customers.txt");
    if (!inFile)
    {
        cout << "File not found!" << endl << endl;
        system("pause");
        return 1;
    }
    customer x;
    inFile >> numCustomers;
    while (!(inFile.eof())) {
        inFile >> n >> p >> h;
            x.setName(n);
            x.setPlan(p);
            x.setHours(h);
    }
    x.printArray();
    inFile.close(); 
}

0 个答案:

没有答案