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);
}
}
}
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.
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:
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.
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.