Here are program instructions.

IT SAYS "segmentation fault" WHEN RAN.

Specifications

You must create a Ship class:
The Ship class maintains the position data for each ship/vessel and its distance from another Ship.
It has a default constructor, which sets all of the member data to zero, and a non-default constructor that has three parameters, one for each coordinate.
It contains methods to calculate the distance to another Ship and to retrieve that distance later.
You must also overload a relational operator, such as < or >, for use in a sorting routine. Thus, a Ship object can report whether or not its distance is less than or greater than that of another Ship object.
When you input the position data for your warship, you must use that data to create a Ship object dynamically.
After reading in the number of enemy vessels, you must dynamically create an array of Ship objects to hold the enemy vessel data.
You must create a separate function for reading in the data for the enemy vessels.
After you read in the data for each enemy vessel, you must use the non-default constructor to create a Ship object and then assign it to a slot in the array. You will then call the method to calculate the distance from the warship.
After all of the data is read, you must sort the enemy vessels by their distances from the warship. You must write a separate function for sorting.
You must output the distances in sorted order. You must write a separate function to output this data.
Sample position data is available in a file named positions.txt. Each line in the file contains a single position, with each coordinate separated by a space. For example, the line of data:
10 30 85
represents the position (10,30,85) in x,y,z coordinates. The first line in the file contains the position of your warship, the next line contains the number of enemy vessels, and the remaining lines each contain the position of a single enemy vessel.
You must deallocate all dynamically-allocated memory.
Output

Your output should look as follows:

There are 7 enemy vessels

Sorted distances:
47
55
76
85
100
198
425

main.cpp

#include <iostream>
#include "Ship.h"
using namespace std;

int main()
{
	int x, y, z;
	int amount;
	cout << "Please enter your ship data" << endl;
	cin >> x;
	cin >> y;
	cin >> z;
	Ship * myLocation = new Ship(x, y, z);
	
	cout << "How many ships are there?" << endl;
	cin >> amount;
	Ship * ships = new Ship[amount];
	
	for (int i = 0; i < amount; i++)
	{
		ships[i] = ships[i].getCoordinates();
	}
		
	for (int j = 0; j < amount; j++)
	{
		ships[j].setDistance(*myLocation, ships[j]);
	}
	

	ships[amount].sortDistance(&ships[amount], amount);
	ships[amount].outputDistance(&ships[amount], amount);
	
	return 0;
}

ship.h

class Ship
{
protected:
	int x, y, z;
	float distance;
	Ship * ships;
public:
	Ship();
	Ship(int, int, int);
	~Ship();
	
	Ship & getCoordinates();
	void setDistance(Ship, Ship);
	float getDistance();
	bool operator<(Ship &);
	void sortDistance(Ship *, int);
	void outputDistance(Ship *, int);
	
};

ship.cpp

Ship::Ship():x(0), y(0), z(0), distance(0)
{
}

Ship::Ship(int init_x, int init_y, int init_z): x(init_x), y(init_y), z(init_z)
{
}

Ship::~Ship()
{
	delete [] ships;
}

Ship & Ship::getCoordinates()
{
	cout << "Please eneter the coordinates of ship" << endl;
	cin >> x;
	cin >> y;
	cin >> z;
	Ship * s = new Ship(x, y, z);
	return *s;
}

void Ship::setDistance(Ship s1, Ship s2)
{
	distance = sqrt(((float)(s1.x - s2.x)*(s1.x - s2.x)) + ((s1.y - s2.y)*(s1.y - s2.y)) + ((s1.z - s2.z)*(s1.z - s2.z)));
}

float Ship::getDistance()
{
	return distance;
}

bool Ship::operator<(Ship & other)
{
	if (distance < other.distance)
		return true;
	else
		return false;
}

void Ship::sortDistance(Ship * new_ships, int number)
{
	for (int i = 0; i < number - 1; i++)
	{
		for (int j = 0; j < number; j++)
		{
			if (new_ships[i] < new_ships[j])
			{
				Ship temp;
				temp = new_ships[i];
				new_ships[i] = ships[j];
				new_ships[j] = temp;
			}
		}
	}
}

void Ship::outputDistance(Ship * new_ships, int number)
{
	for (int i = 0; i < number; i++)
	{
		cout << "This is what ship " << i << " should be" << endl;
	}
}

Edited 4 Years Ago by Adnan671: n/a

Post on which line number you are getting segmentation fault. Paste the exact message here, you might get quicker replies

Completely depends on what's wrong. This will help...

[boilerplate_help_info]

Posting requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information.
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]Do [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*]Do not apologize for posting 'late'. We don't have any expectations on when you should be posting - 10 minutes or 10 days. We aren't timing your responses.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*]Do not attach files except when absolutely necessary. Most of us are not going to download files.  Add the information to your post.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[*]Create a [b][i]good[/i][/b] title for your post. The title [b]C++[/b] in the C++ forum is bloody redundant and worthless!  [b]What's wrong?[/b] equally so. Specifically what are you having trouble with? [i]There[/i] is your title. [i](note: [b]my program[/b] is not the answer.)[/i]
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

This article has been dead for over six months. Start a new discussion instead.