Planets program---URGENT!

Hey guys, need some help in figuring how to write this program. The program should have a menu with the following:

1)Add planet (prompt for name, radius, mass)
2)Delete planet (prompt for name)
3)Find planet (display name, radius, mass, surface area, volume, and gravity at surface)
4)List all planets (using vectors)
5)Sort by name (alphabetical)
6)Quit

I think I got how to put the list to show up with the following code but need some help with how to make classes and vectors.

Any help would be great, kinda lost.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

#include <iostream>
#include <string>
#include <cmath>
#include <vector>


using namespace std;

int choice = 0;
double name, radius, mass, SurfaceArea, volume, density, gravity;


int main()
{
	while (choice != 6)
	{
		//Menu
		cout << "Planets Program" << endl;
		cout << "1) Add Planet" << endl;
		cout << "2) Delete Planet" << endl;
		cout << "3) Find Planet" << endl;
		cout << "4) List All Planets" << endl;
		cout << "5) Sort By Name" << endl;
		cout << "6) Quit" << endl;
		cout << "Enter your choice (1-6):";
		cin >> choice;

		if (choice == 1)
		{
			cout << "Please enter the name of the planet:";
			cin >> name;
			cout << "Enter the radius:";
			cin >> radius;
			cout << "Enter the mass:";
			cin >> mass;
		}

		else if (choice == 2)
		{
			cout << "Please enter the name of planet you want to delete:";
			cin >> name;
		}

		else if (choice == 3)
		{
			//Display name, radius, mass, surface area, volume, density and gravity
		}

		else if (choice == 4)
		{
			//Use vectors to list all planets
		}

		else if (choice == 5)
		{
			//Sort by name (alphabetically)
		}

		else if (choice == 6)
		{
			cout << "Goodbye" << endl;
		}

		else {
			cout << "Error, please enter value between 1-6" << endl;
		}

	}
	return 0;
}

Can anyone help me out??
just create a class called Planet.
all the variables on your line 11, move into this class (name should be a std::string and NOT a double though).

then in your main create a vector:
 
std::vector<Planet> planets;


and add to this. when you sort use
http://www.cplusplus.com/reference/algorithm/sort/

will have to maybe write a small comparator to sort on planet name.
thanks, I will try that and post it back so you can check if I did it right
I noticed that you are not deleting a planet on the option "2)Delete Planet".

You could use dynamic memory allocation and use delete.

You could also replace the place it takes up in the vector with a Planet object that is empty.

Hopefully this helps.
no need to use pointers. just call erase on the vector after searching for the name string in the vector collection.
quick question, should I put all the variables (mass, density, etc) that I want to display in private or public (under class Planet). How do I make it so that when I enter Earth, it displays all the properties for that planet?

make them public now, just to get something working.
worry about accessors and stuff like that later on.

That would be my opinion.
hey guys, i have tried to figure this out, I don't know if my class is right and am lost on how to display the parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

int choice = 0;
string Planet_Name;
double radius, mass, surface_area, volume, density, gravity;


class Planet
{
public:
	Planet();
	bool Add(double radius, double mass); //adds a planet
	bool Delete(vector<string>planets); //removes a planet
	void Sort(); //sort function
	void Find(); // finds a planet
	void Display(); //display all the link list
	double surface_area;
	double density;
	double mass;
	double radius;
	double volume;
	double gravity;
	
};


int main()
{
	vector<Planet> plist;
	for (int i = 0; i < 9; i++) {
		Planet p;
		cout << "radius?";
		cin >> p.radius;
		cout << "mass?";
		cin >> p.mass;
		plist.push_back(p);
	}

	
	Display(plist);
	Sort(plist);
	Display(plist);



	
	while (choice != 6)
	{
		//Menu
		cout << "Planets Program" << endl;
		cout << "1) Add Planet" << endl;
		cout << "2) Delete Planet" << endl;
		cout << "3) Find Planet" << endl;
		cout << "4) List All Planets" << endl;
		cout << "5) Sort By Name" << endl;
		cout << "6) Quit" << endl;
		cout << "Enter your choice (1-6):";
		cin >> choice;

		if (choice == 1)
		{
			cout << "Please enter the name of the planet:";
			cin >> Planet_Name;
			cout << "Enter the radius:";
			cin >> radius;
			cout << "Enter the mass:";
			cin >> mass;
		}

		else if (choice == 2)
		{
			cout << "Please enter the name of planet you want to delete:";
			cin >> Planet_Name;
		}

		else if (choice == 3)
		{
			cout << "Which planet do you want to find?";
			cin >> Planet_Name;
			//Display name, radius, mass, surface area, volume, density and gravity
		}

		else if (choice == 4)
		{
			//Use vectors to list all planets
		}

		else if (choice == 5)
		{
			//Sort by name (alphabetically)
		}

		else if (choice == 6)
		{
			cout << "Goodbye" << endl;
		}

		else {
			cout << "Error, please enter value between 1-6" << endl;
		}

	}
	return 0;
}
looking okay. now you need to read up on some vector methods like:
1. push_back() - for adding a planet to your collection
2. erase() to remove one.
3. also about finding the correct planet in your collection (so you delete the right one).

also after line 73 you need to create a planet object, assign your name, radius and mass into it (possibly via the constructor?), then push this onto your vector (using push_back() ).

delete line 11, move line 10 into your class as a member variable.

All of your methods deal with the collection of planets, rather than a single planet these should NOT be in the planet class.
Last edited on
Topic archived. No new replies allowed.