我不太明白为什么我的程序会打印出奇怪的数字,我相信它是地址数字......我试图从文件中读取数据,然后将它们存储在类实例中......文件有id,x,y,z 在一行中....它有 10 行,因此我要创建 10 个类实例..很高兴您的帮助...^^
class planet
{
public:
int id_planet;
float x,y,z;
};
void report_planet_properties(planet& P)
{
cout<<"Planet's ID: "<<P.id_planet<<endl;
cout<<"Planet's coordinates (x,y,z): ("<<P.x<<","<<P.y<<","<<P.z<<")"<<endl;
}
planet* generate_planet(ifstream& fin)
{
planet* p = new planet;
fin >> (*p).id_planet;
fin >> (*p).x;
fin >> (*p).y;
fin >> (*p).z;
return (p);
}
int main()
{
planet* the_planets[10];
int i=0;
ifstream f_inn ("route.txt");
if (f_inn.is_open())
{
f_inn >> i;
for(int j=0;j<i;j++)
{
the_planets[j]=generate_planet(f_inn);
report_planet_properties(*the_planets[i]);
delete the_planets[j];
}
f_inn.close();
}
else cout << "Unable to open file";
}