我不明白爲什麼我的程序打印出奇怪的數字,我相信這是地址編號....我試圖從文件中讀取數據,然後將它們存儲在類實例中。 ...文件的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";
}
對不起....代碼行「f_inn >>我」這是因爲在文件中的第一行顯示我需要創建類THT的數量... –