I am having a slight problem in the main programme outputing the values of the pointer to the array returned by the function generate_planet(ifstream &fin) [Line 56]. At the moment, I can only return the first (of four values) of the array the_planets [Line 92] for every counter increment of 'i' within the main programme. How can I output all the four values of the_planet within the main programme
Yes, there are some problems with your code.
The problems are numerous and intertwined.
It looks like an effort to "force" the program to work has been made.
For starters:
1 2 3 4
#include <iostream>
#include <cmath>
#include <fstream>
usingnamespace std;// put this here. No need for it to appear anywhere else.
Next:
You declare (but never use) an array of 10 planet pointers. I think it's these pointers which
the function generate_planet() is meant to provide values for. It would work if generate_planet allocates an instance of planet, fills it with values from the file then returns a pointer to the planet.
I think the program would work better if you used the following:
1 2 3 4 5 6
ifstream fin;
fin.open ("route.txt");// add code to test for failure here before continuing with the file read
fin >> temp_size;
planet **the_planets = new planet*[temp_size];// creates an array of temp_size planet pointers
for (i=0; i<temp_size; i++)
the_planets[i] = generate_planet (fin);
Where:
1 2 3 4 5 6 7 8 9
planet* generate_planet(ifstream &fin)
{
planet* new_planet = new planet;
fin >> new_planet->planet_id;
fin >> new_planet->x;
fin >> new_planet->y;
fin >> new_planet->z;
return new_planet;
}
Then:
1 2
for (i=0; i<temp_size; i++)
report_planet( the_planets[i] );// passing a pointer to planet i
Brilliant! it works; Thank you. Yours is an excellent interpretation of what I was attempting to do, without any issues and it looks so simple. Can you advise me on any books that I can use to polish my skills in C++ programming, including good coding practice?