Adding and Deleting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dudeman94
    New Member
    • Dec 2014
    • 1

    #1

    Adding and Deleting

    i am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area(sphere formula) and the density of the planet. along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part.
    the code the adding and deleting from the list would go in cases 9 and 10.

    heres my code as of this point.

    Code:
    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    #include <set>
    using namespace std;
    void print(const std::string& item)
    {
        std::cout << item << std::endl;
        std::set<std::string> sortedItems;}
    
    int main()
    {
        int choice;
        bool gameOn = true;
        while (gameOn != false){
            cout << "*******************************\n";
            cout << " 1  - Murcury\n";
            cout << " 2  - Venus\n";
            cout << " 3  - Earth\n";
            cout << " 4  - Mars\n";
            cout << " 5  - Jupiter\n";
            cout << " 6  - Saturn\n";
            cout << " 7  - Uranus\n";
            cout << " 8  - Neptune\n";
            cout << " 9  - Add A Planet\n";
            cout << " 10 - Delete A Planet\n";
            cout << " 11 - Sort\n";
            cout << " 12 - Quit\n";
            cout << "*******************************\n";
            cout << " Enter your choice and press return: ";
        
            cin >> choice;
            
            switch (choice)
            {
                {case 1:
                   cout<<"~~~~Murcury~~~~\n";
                    cout<<"The Mass is 3.3022x10^23 KG.\n";
                    cout<<"The Radius is 4,880 KM.\n";
                    double mass=3.3022*pow(10,23);
                    double rad=4880;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                    cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 2:
                    cout<<"~~~~Venus~~~~\n";
                    cout<<"The Mass is 4.8685x10^24 KG.\n";
                    cout<<"The Radius is 12,103.6 KM.\n";
                    double mass=4.8685*pow(10,24);
                    double rad=12103.6;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                    cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 3:
                    cout<<"~~~~Earth~~~~\n";
                    cout<<"The Mass is 5.972x10^24.\n";
                    cout<<"The Radius is 12,756.3  KM.\n";
                    double mass=5.972*pow(10,24);
                    double rad=12756.3 ;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                    cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 4:
                    cout<<"~~~~Mars~~~~\n";
                    cout<<"The Mass is 6.4219x10^23 KG.\n";
                    cout<<"The Radius is 6,794KM.\n";
                    double mass=4.8685*pow(10,23);
                    double rad=6794;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                   cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 5:
                    cout<<"~~~~Jupiter~~~~\n";
                    cout<<"The Mass is 1.900ex10^27 KG.\n";
                    cout<<"The Radius is 142984KM.\n";
                    double mass=4.8685*pow(10,27);
                    double rad=142984;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                    cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 6:
                    cout<<"~~~~Saturn~~~~\n";
                    cout<<"The Mass is 5.68x10^26 KG.\n";
                    cout<<"The Radius is 120,536 km.\n";
                    double mass=5.68*pow(10,26);
                    double rad=120536;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                   cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 7:
                    cout<<"~~~~Uranus~~~~\n";
                    cout<<"The Mass is 8.683x10^25 KG.\n";
                    cout<<"The Radius is 51,118 KM.\n";
                    double mass= 8.683*pow(10,25);
                    double rad=51118;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                    cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 8:
                    cout<<"~~~~Neptune~~~~\n";
                    cout<<"The Mass is 1.0247x10^26 KG.\n";
                    cout<<"The Radius is 49,532 KM.\n";
                    double mass=1.0247*pow(10,26);
                    double rad=49532;
                    double volumeSphere= (4/3)*M_PI*pow(rad,3.0);
                    double density=mass/volumeSphere;
                    cout<<"The Density of Murcury is:: "<<density<<"\n";
                    double SurASphere= 4*M_PI*pow(rad,2.0);
                    cout<<"The Surface Area:: "<<SurASphere<<"\n";
                    break;}
                {case 9:
                    
                    break;}
                    
                {case 10:
                    
                    break;}
              
                {case 11:
                   
                    std::set<std::string> sortedItems;
                    for(int i = 1; i <= 8; ++i)
                    {
                        std::string name;
                        std::cout << i << ". ";
                        std::cin >> name;
                        
                        sortedItems.insert(name);
                    }
                    
                    std::for_each(sortedItems.begin(), sortedItems.end(), &print);
    
                    break;}
                {case 12:
                    cout << "End of Program.\n";
                    gameOn = false;
                    break;}
                {default:
                    cout << "Not a Valid Choice. \n";
                    cout << "Choose again.\n";
                    cin >> choice;
                    break;}
            }
            return 0;
        }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I don't see a list of planets. Instead all your data is hard coded in the switch(choice) cases.

    You will need to develop a class Planet and then create 8 planet objects.

    Then you need a list of planets so use std::list and add your 8 planet objects to the list. The list has methods to add and delete from the list. Also there is a sort method but you will need to write a function of compare two Planet objects.

    Then all you have to do is iiterate the list and display what's in it.

    What do you think?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What list are you talking about? At first, I thought you meant the list of planet choices offered to the user, but if you did that then how would you add planets to the list?

      You repeat the formulas for volume-of-a-sphere, surface-area-of-sphere, and density for each of the eight planets. It would be better if each formula were encapsulated in a function (or method?).

      You are using C++. Should you define a planet object that is instantiated for each of the eight planets?

      The mass and radius of each planet is hard-coded twice for each planet. You should never require yourself to type in the same nonintuitive number in two different places.

      You are not using the usual way to specify floating point constants. I don't know about C++, but including a function call in an initializer won't work for [most versions of] C.
      Code:
      Replace this:
      double mass=5.972*pow(10,24);
      with this:
      double mass = 5.972e24;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code:
        Code:
        double mass=5.972*pow(10,24);
        is OK in C++.

        The list I was talking about is the C++ list container std::list<>:

        Code:
        std::list<Planet> var;

        Comment

        Working...