|
-
October 3rd, 2009, 06:08 PM
#1
Runtime error, Unknown problem. (Homework)
Hi everyone.
Im new to this forum and hoping for a little help. I have done some C programming before, and now am doing a C++ course in my second undergrad year, but having a little trouble with this one program. Id appreciate any help that you could offer me as I have tried everything that I can possibly think of to try and get it working.
So, what the program does:
Set up a class describing a planet containing x,y,z and an id.
Create an array to point to these planets (10 of).
Use a function to take co-ordinates from a text file, add it to a created class and then pass it back into the array.
Display the co-ordinates on screen to prove it works.
Work out the total distance between all of these planets.
So, whats wrong:
The program compiles correctly, works for the first three lines of the text file, then promptly tells me there is a problem, quitting the program. I think, but cannot be sure, that the problem occurs when the generate_planet function is called and creates a new planet class.
The code:
Code:
int main(void)
{
int i = 0;
float total_distance = 0.0;
ifstream fin;
fin.open("route.txt");
planet* the_planets[10];
for( i = 0 ; i < 2 ; i++ )
{
*the_planets[i] = generate_planet(fin);
report_planet(*the_planets[i]);
}
for( i = 0 ; i <= 2 ; i++)
{
total_distance += distance(&the_planets[i], &the_planets[i+1]);
}
/*
planet mars, earth;
get_planet(&mars);
get_planet(&earth);
cout << "distance is " << distance(&mars, &earth);
return 0; */
}
void report_planet(planet report)
{
cout << "Co-ordinates for " << report.id << endl;
cout << "x : " << report.x << endl;
cout << "y : " << report.y << endl;
cout << "z : " << report.z << endl;
}
void get_planet(planet *get)
{
cout << "Please enter planet ID\n";
cin >> get->id;
cout << "Please enter x co-ordinate\n";
cin >> get->x;
cout << "Please enter y co-ordinate\n";
cin >> get->y;
cout << "Please enter z co-ordinate\n";
cin >> get->z;
}
float distance(planet *planet1, planet *planet2)
{
float distance=0.0;
distance = sqrt( pow((planet1->x - planet2->x),2) + pow((planet1->y - planet2->y),2) + pow((planet1->z - planet2->z),2) );
return distance;
}
planet &generate_planet( ifstream &fin )
{
planet *newplanet = new planet;
fin >> newplanet->id >> newplanet->x >> newplanet->y >> newplanet->z;
return *newplanet;
}
The text file:
Code:
1 9 8 6
2 6 8 2
3 7 8 1
4 1 1 2
5 21 11 1
6 7 11 0
7 11 18 1
8 5 3 1
9 9 9 17
10 8 1 1
Thanks for any help, I really am at a loss to where the problem occurs in this program and I appreciate any comments about any part of the code, but in particular the section that is not working.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|