做习题时遇到的问题
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct juan
{
string name;
double qian;
};
int main()
{
ifstream inFile;
inFile.open("scores.txt");
cout << "Enter the number of juan: ";
int u;
inFile >> u; //txt文件第一排第一个东西就是一个数字 4
cout << u << endl;
inFile.get(); //尝试了两个小时,最后加上这个才算正常运行,但是为什么一定要加啊?
juan *ch = new juan[u];
juan *sh = new juan[u];
juan *hh = new juan[u];
int i = 0;
int c = 0;
int y = 0;
while (inFile.good())
{
getline(inFile, ch[i].name);
inFile >> ch[i].qian; //上面第一次读取的u后面加了一条inFile.get()才行,而这里又不能加,同样都是读取的数字,为什么后面的加上了inFile.get()反而运行错误了呢?
if (ch[i].qian > 10000)
{
sh[c].qian = ch[i].qian;
sh[c].name = ch[i].name;
c++;
}
else
{
hh[y].name = ch[i].name;
hh[y].qian = ch[i].qian;
y++;
}
i++;
getline(inFile, ch[i].name);
}
cout << "Grand Patrons \n";
for (int x = 0; x < c; x++)
cout << sh[x].name << ":" << sh[x].qian << endl;
cout << "Patrons \n";
for (int x = 0; x < y; x++)
cout << hh[x].name << endl;
return 0;
}