如何阅读条件为

时间:2015-07-07 20:25:40

标签: c++ struct

我的应用程序很简单,但仍然有点代码,所以我只是分解它。例如,我的文本文件看起来像这样

John Doe
British
2
Swimming test
Soccer

我的结构非常基础:

string name;
char national [MAX];
int noOfHobbies;
char hobby [MAXNO][MAX];

然后我有一个函数打开这个文件,以便读取数据并成功管理读取它,但问题是它没有输出" Soccer"作为第二个爱好但是"测试"。我的功能基本上看起来像这样:

................................

const int MAXRECS=10;
MyInfo infoList[MAXRECS];
while( getline(afile, line) ) {
    MyInfo temp; // MyInfo is a structure
    temp.name = line; 
    afile >> temp.national;
    afile >> temp.noOfWishes;
    for(int j = 1; j <= temp.noOfWishes; j++) {
        afile >> temp.wish[j];
    }
}

..............................................
cout << "I have " <<  infoList[i].noOfHobbies<< " hobbies" << endl; // output "I have 2 hobbies"
for(int j = 1; j <= infoList[i].noOfHobbies; j++) {
        cout << j << ": " << infoList[i].hobby[j] << '\n'; // output "Swimming" and "Test"
}   

为什么在此处将空格读为新行,因为它适用于namenational但不适用hobby?我是C ++的新手,所以如果这是一个基本问题,我希望你能同情我。

0 个答案:

没有答案
相关问题