I need help - outputing the values of an array returned from a function

Hello,

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

The content of the input file (route.txt) is:

10
1 9 8 10
2 6 8 3
3 7 8 1
4 1 1 2
5 21 11 6
6 7 11 0
7 11 18 1
8 5 3 1
9 9 9 17
10 8 1 2

The programme I have developed so far is as follows:

#include <iostream>
#include <cmath>
#include <fstream>

class planet
{
public:
// Variables that define the coordinates of the planets
float planet_id;
float x;
float y;
float z;
};

void report_planet (float planet_id, float x_coord, float y_coord, float z_coord)
{
using namespace std;

cout << "Planet's ID: " << planet_id << endl;
cout << "x coordinate: " << x_coord << endl;
cout << "y coordinate: " << y_coord << endl;
cout << "z coordinate: " << z_coord << endl;
cout << "\n";

return;
};

using namespace std;

float generate_planet (ifstream &fin)
{
int j = 0;
float *temp_arr;
temp_arr = new float [4];
for (j=0; j<4; j++)
{
fin >> temp_arr[j];
cout << temp_arr[j] << "\t";
cout << j << endl;
}
return *temp_arr;
delete [] temp_arr;
};

void main()
{
using namespace std;

int i;
int j;
int temp_size;
planet *the_planets[10];

ifstream fin;
fin.open ("route.txt");
fin >> temp_size;
float *temp_array = new float[temp_size];
for (i=0; i<temp_size; i++)
{
float the_planets = generate_planet (fin);
cout << "\n";
cout << the_planets << endl;
report_planet(the_planets, 12, 32, -93);
cout << "\n";
}

delete [] temp_array;
fin.close ();

cin.sync ();
cin.get ();

return;
}
Last edited on
closed account (D80DSL3A)
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>
using namespace 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 

Where:
1
2
3
4
5
6
7
8
9
void report_planet(planet* a_planet)
{
	cout << "Planet's ID: " <<  a_planet->planet_id << endl;
	cout << "x coordinate: " << a_planet->x << endl;
	cout << "y coordinate: " << a_planet->y << endl;
	cout << "z coordinate: " << a_planet->z << endl;
	cout << "\n";
	return;
}

Finally, release all the memory:
1
2
3
for (i=0; i<temp_size; i++)
	delete the_planets[i];// delete each planet
delete [] the_planets;// delete the array of pointers 


EDIT: Note that planet** the_planets is to replace planet* the_planets[10].

Last edited on
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?
Last edited on
Hi, you should use objects instead of pointers.

1
2
fin >> temp_size;
std::vector<planet> the_planets(temp_size);// creates an array of temp_size planets 


1
2
3
4
5
6
planet generate_planet(ifstream &fin)
{
	planet new_planet;
	//fill the data
	return new_planet;
}


Also: main must return int
Thanks Ne555! I will try the implementation for array creation that you have suggested.
Topic archived. No new replies allowed.