reading text file to a class type variable

i am new to c++ and i'm try to write a code that reads integers of a text file and saves each integer in different variables in a class.I am having problems with the syntax and how to arrange the code. Basically the text file contains 4 integers per line which values are to be read to the a class planet's coordinates and ID as shown below. I know the code below is incomplete but this is the first time im programming with c++ and need help. please you do not need to explain this using planets or anything. I just need a general understanding

[code#include <iostream>
#include <fstream>



using namespace std;

class planet{
public :
float x_coordinates;
float y_coordinates;
float z_coordinates;
int id;
};




planet* generate_planet(istream &fin)
{
planet *the_planet= new planet();
fin >>the_planet->id>>the_planet->x_coordinates>>the_planet->y_coordinates>>the_planet->z_coordinates;

return (the_planet);
}
void report_planet(planet &p)
{

cout<<"planet "<< p.id <<" has coordinates (" << p.x_coordinates<<","<< p.y_coordinates<<","<< p.z_coordinates<<")"<<endl;
}
int main()
{
planet p;
ifstream fin("route.txt");
generate_planet(fin);
report_planet(p);


return 0;
}
[/code]
Last edited on
first off, add a space between your planet* and generate_planet(istream &fin). The compiler treats this as a pointer to a generate_planet object(instead of treating it as a function name).

secondly, you never delete your dynamically allocated object in generate_planet, this causes memory leaks and can be extremely bad for your program. Make sure you delete it at some point in the program(end preferably).

You might want to think about looking over the concepts of c++ more, i see a lot of logical and semantics errors. Public variables...memory leaks...strange functions...etc...etc..

okay, i will reviewmy code again/ thank you
Topic archived. No new replies allowed.