How do I create a vector that the user can keep ad

2019-06-13 03:01发布

Our homework assignment is to create a a menu where the user has the following options:

  1. Add a planet
  2. Delete planet
  3. Find planet (by name)
  4. List all planets
  5. Sort (alphabetical order)
  6. Quit

The requirements are that the planets must be stored as a class with name, diameter, mass as private members and the methods of the class being the density, surface area, and gravity. However I realized that I will need to use a vector to dynamically keep adding the planets as the user keeps entering them. How would I go about creating such a vector? And because I have to specifically use classes, where would the code for creating the vector go? Would it be at the beginning of int main() with a function inside to access the values (name, diameter, mass) entered by the user?

This is what I have so far:

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

using namespace std;

class planet
{
    private:
        string n;
        double d, m;
    public:
        void Density (double d, double m)
        {
            double Den = m/((4.0/3.0)*M_PI*pow((d/2.0), 3.0));
            cout<<"Density: "<<Den<<endl;
        }
        void SurfaceArea(double d)
        {
            double S = 4.0*M_PI*pow((d/2.0), 2.0);
            cout<<"Surface Area: "<<S<<endl;
        }   
        void Name (string n)
        {
            string N = n;
            cout<<"Name: "<<N<<endl;
        }
        void Gravity (double G, double m, double d)
        {
            double F = G*m/pow((d/2.0), 2.0);
            cout<<"Force of gravity: "<<F<<endl;
        }

};

int main()
{
    const double G=6.67384e-11;
    int c=0;
    string n;
    double d=0.0, m=0.0, Den=0.0, S=0.0;

    do 
    {
        cout<<"1. Add a planet\n";
        cout<<"2. Delete planet\n";
        cout<<"3. Find planet (by name)\n";
        cout<<"4. List all planets\n";
        cout<<"5. Sort (alphabetical order)\n";
        cout<<"6. Quit\n";
        cout<<endl;
        cout<<"Please select an option from the menu above."<<endl;
        cin>>c;

        if(c==1)
        {
            planet red;

            cout<<"Enter the planet's name: ";
            cin>>n;
            cout<<"Enter the planet's diameter: ";
            cin>>d;
            cout<<"Enter the planet's mass: ";
            cin>>m;

            red.Name(n);
            red.Density(d, m);
            red.SurfaceArea(d/2.0);
            red.Gravity(G, m, d);



        }
        else if (c==4)
        {
            cout<<N<<endl;
            cout<<Den<<endl;
            cout<<S<<endl;
            cout<<F<<endl;
        }


    } while (c!=6);

    system("pause");

    return 0;
}

If I need to clarify more please let me know nicely :)

Thanks!

标签: c++ vector menu
1条回答
▲ chillily
2楼-- · 2019-06-13 03:32

You would need to create a vector like this vector<planet> p to create a vector that can store your class planet. You'd want to create this vector at the start of the program so that it is ready to get filled as soon as users start entering data.

To add a planet, just push_back the planet. To delete, use the vector's erase function.

To find a planet in your vector, just iterate through your vector with a for loop and see if p.at(i).n == planet_to_find .

To print all the planets, like the find loop, iterate through your planets and cout << p.at(i).n.

To sort, you can use the std sort function to sort alphabetically. Details on sort.

I hope this helps :)

查看更多
登录 后发表回答