Final Project: Stuck on adding planets and its data

This is my final project assignment:
Write a C++ program that will help astronomers keep track of planets. Your program should use a class that has members to store the planets name, diameter (or radius if you prefer) and mass. I should also have functions to calculate the surface area, density and acceleration due to gravity (g).

Give the user a menu similar to the one shown below:

1. Add Planet

2. Search for Planet

3. Delete a Planet

4. Display All Planets

5. Sort Planets

6. Quit
The Sort function will sort the list alphabetically. Note - If you are using an insert sort to add the planets to the list, this function is not required.


As of now I'm able to enter values and have it the options function but I'm unable to get it to work with strings, (planet names). After that I have no idea what to do for the data that I'm supposed to calculate and then display.
Note: the display part is wrong, it's just a placeholder for now.

I'm new to programming so I very little experience and knowledge to a lot of this. If you're able to answer in a simple manner that will be appreciated.

This is my program so far:





#include<iostream>
#include <cstdlib>
using namespace std;

void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;

int main()
{
int ch,y;
for(i=1;i<40;i++)
tree[i]=-1;
while(1)
{
cout <<"1.Add planet\n";
cout<<"2.Delete planet\n";
cout<<"3.Display planets\n";
cout<<"4.Search planet\n";
cout<<"5.Quit Program\n";
cout<<endl;
cout<<"Enter your choice: ";

cin >> ch;
switch(ch)
{
case 1: //Adds planet
cout <<"enter the element to insert: ";
cin >> ch;
insert(1,ch);
cout<<endl;
break;
case 2: //Delete planet
cout <<"enter the element to delete: ";
cin >>x;
y=search(1);
if(y!=-1) delte(y);
else cout<<"no such element in tree: ";
cout<<endl;
break;
case 3: //Display
display(1);
cout<<"\n";
for(int i=0;i<=32;i++)
cout <<i;
cout <<"\n";
cout <<endl;
break;
case 4: //Search
cout <<"enter the element to search: ";
cin >> x;
y=search(1);
if(y == -1) cout <<"no such element in tree";
else cout <<x << " is in " <<y <<" position";
cout<<endl;
break;
case 5: //quits
exit(0);
}
}
}

void insert(int s,int ch )
{
int x;
if(t==1)
{
tree[t++]=ch;
return;
}
x=search1(s,ch);
if(tree[x]>ch)
tree[2*x]=ch;
else
tree[2*x+1]=ch;
t++;
}
void delte(int x)
{
if( tree[2*x]==-1 && tree[2*x+1]==-1)
tree[x]=-1;
else if(tree[2*x]==-1)
{ tree[x]=tree[2*x+1];
tree[2*x+1]=-1;
}
else if(tree[2*x+1]==-1)
{ tree[x]=tree[2*x];
tree[2*x]=-1;
}
else
{
tree[x]=tree[2*x];
delte(2*x);
}
t--;
}

int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}

void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}

int search1(int s,int ch)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}
Last edited on
Planet names are strings, not integers, so an array of integers is not going to be of much use in this program. Also, to sort the name of the planets as they go in and avoid duplicates, you can store the names in a std::set<string> and this takes care of your instruction
If you are using an insert sort to add the planets to the list, this function is not required.
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
#include <iostream>
#include <string>
#include <set>

using namespace std;

int main()
{
    set<string> Planets;
    bool fQuit = false;

    while(!fQuit)
    {
        cout << "1. Add Planet \n2. Delete a Planet \n3. Display All Planets \n4. Search for Planet \n5. Quit\n";
        int choice;
        cin >> choice;

        switch(choice)
        {
            case 1:
            {
                cout << "Enter planet name: \n";
                string name;
                cin >> name;
                if(Planets.insert(name).second)
                {
                    cout << name << " added\n";
                }
                else
                {
                    cout << name << "already exists\n";
                }
            }
            break;
            case 2:
            {
                cout << "Enter planet name to delete: \n";
                string name;
                cin >> name;
                auto search = Planets.find(name);
                if(search != Planets.end())
                {
                    Planets.erase(name);
                    cout << name << " deleted \n";
                }
                else
                {
                    cout << "Not found \n";
                }
            }
            break;
            case 3:
            {
                if(Planets.empty())
                {
                    cout << "No planets found \n";
                }
                else
                {
                    for (auto & elem : Planets)
                    {
                        cout << elem << " " << '\n';
                    }
                }
            }
            break;
            case 4:
            {
                cout << "Enter planet name to search: \n";
                string name;
                cin >> name;
                auto search = Planets.find(name);
                if(search != Planets.end())
                {
                    cout << "Found " << (*search) << '\n';
                }
                else
                {
                    cout << "Not found \n";
                }
            }
            break;
            case 5:
                fQuit = true;
                cout << "Goodbye \n";
            break;
            default:
            cout << "Wrong choice, try again \n";
            break;
        }
    }
}

PS: currently the set sorts the planet by name but you can also write a custom comparison function to sort by size, distance from sun etc if you wish
Ah okay. That's when I would get stump. So since I'm asking for a name, diameter, and mass I'll be needing three arrays, right? Or am I viewing this wrong and they all go into one array? How can I execute this? I would then wonder how I can display these.
Apologies If I'm sounding too repetitive or annoying, I just want to really understand the concept and make my program work.

P.S: Thank you for wording it in a way that's easy for me to understand. I greatly appreciate it as well as the advice and tips you've given me.
To record name, diameter and mass for each planet you'll need the additionals in the following approach:
1. a struct Planet with data members name, diameter, mass
2. overloaded insertion operator << to print Planet objects
3. a custom comparator so that Planets are sorted by name as they go into the set
4. a custom predicate to search the set by name of planets (you could do 3 and 4 by lambdas as well)
Putting them all together:

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
#include <set>
#include <algorithm>

using namespace std;

struct Planet
{
    string m_name;
    double m_diameter;//e.g radius of Jupiter 69911 km, diamter 2*radius??
    double m_mass;//e.g mass of Jupiter is 317.8M; //choose other datatype for diameter and mass if reqd
    Planet (string name, double diameter, double mass)
        : m_name (name), m_diameter (diameter), m_mass (mass){}
};
ostream& operator << (ostream& os, const Planet& p)
{
    os << "Name: " << p.m_name << " Diameter: " << p.m_diameter << " Mass: " << p.m_mass <<'\n';
    return os;
}
struct Planet_Compare
{
    bool operator () (const Planet& lhs, const Planet& rhs)
    {
        return lhs.m_name < rhs.m_name;
    }
};
struct find_by_name
{
    string name;

    find_by_name(const string & name) : name(name) {}
    bool operator()(const Planet & p)
    {
        return p.m_name == name;
    }
};

int main()
{
    set<Planet, Planet_Compare> Planets;
    bool fQuit = false;

    while(!fQuit)
    {
        cout << "1. Add Planet \n2. Delete Planet \n3. Display Planets \n4. Search Planet \n5. Quit \n";

        int choice;
        cin >> choice;

        switch(choice)
        {
            case 1:
            {
                cout << "Enter planet name: \n";
                string name;
                cin >> name;
                cout << "Enter planet diameter: \n";
                double diameter;
                cin >> diameter;
                cout << "Enter planet mass: \n";
                double mass;
                cin >> mass;
                Planet p(name, diameter, mass);
                if(Planets.insert(p).second)
                {
                    cout << p.m_name << " added\n";
                }
                else
                {
                    cout << p.m_name << " already exists\n";
                }
            }
            break;
            case 2:
            {
                cout << "Enter planet name to delete: \n";
                string name;
                cin >> name;
                set<Planet>::iterator result = std::find_if(Planets.begin(), Planets.end(),
                                              find_by_name(name));
                if(result != Planets.end())
                {
                    Planets.erase(*result);
                    cout << name << " deleted";

                }
                else
                {
                    cout << name << " not found \n";
                }
            }
            break;
            case 3:
            {
                if(Planets.empty())
                {
                    cout << "No planets found \n";
                }
                else
                {
                    for (auto & elem : Planets)
                    {
                        cout << elem;
                    }
                }
            }
            break;
            case 4:
            {
                cout << "Enter planet name to search: \n";
                string name;
                cin >> name;
                set<Planet>::iterator result = std::find_if(Planets.begin(), Planets.end(),
                                              find_by_name(name));
                if(result != Planets.end())
                {
                    cout << name << " found \n";
                }
                else
                {
                    cout << name << " not found \n";
                }
            }
            break;
            case 5:
                fQuit = true;
                cout << "Goodbye \n";
            break;
            default:
            cout << "Wrong choice, try again \n";
            break;
        }
    }
}
OOOH!! Okay! That helps a lot. I understand it better now. I really appreciate it.
I just have two more question (sorry). So for line 102 when you use auto, in my codeblocks program it says the following:

warning: 'auto' changes meaning in C++11; please remove it

but with c++ shell (online compiler) it works just fine. It's something we haven't covered in class so what would another way to express it? (If that makes sense)

And also, stop me if I'm wrong, for the parts when I have to calculate the surface area, density and acceleration due to gravity (g). I just have to create a class meant for calculation purposes, add double functions that will return the calculated answer, right? And then from there when the user searches for a planet I'll have the program search for that specific planet and also display it's calculations along with it.
My apologies if that sounds confusing or totally wrong.

Regardless, I GREATLY appreciate your patience and the help you have given me. I only wish I had someway to really thank you properly.
You can use an iterator instead of the range loop:
1
2
3
4
for (set<Planet>::const_iterator itr = Planets.begin(); itr != Planets.end(); itr++)
{
	cout << *itr;
}


For additional calculations, if you have all the inputs you need, then you can indeed write the functions returning double for surface area, density and g. You don't need to create any additional classes, these can be member methods of the Planet struct or even non-member methods. Just remember to const qualify and pass/return references for your functions wherever feasible. And if you want to display the results of these functions when a specific planet is searched for you can include them in the overloaded << operator. If you need any additional variables for these or other functions then just add them to the struct's data-members.

Finally, if you are going to be using C++ regularly and/or are serious about programming I'd urge you strongly to move to C++11 asap. Code::Blocks is a good choice, it comes with C++11 (and even C++14) compilers pretty much ready to use.
Topic archived. No new replies allowed.