I have to create a program that will prompt a user radius and keep adding them until user says no. Then, the program gotta able to hold those numbers and calculate two things: Surface area and Volume. The program also gotta able to Sort the numbers by its volume(low to high).
And the program will also gotta able to do Displaying all the values that users put(Radius, volume and Surface)

  • I don't understand why in float Sphere::variable() wouldn't save the value...(Seems like I can't even display what I put)

-How do I pass the radius value in float Sphere::variable() to float Sphere::area() and float Sphere::volume()??

Here's my code:

Sphere.cpp
#include <iostream>
#include "Sphere.h"
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>

using namespace std;

Sphere::Sphere()
{

}

float Sphere::variable()
{
    vector<int> list;
    string resp;
    do{
    cout << "radius? " << endl;
    cin >> Sph_radius;
    list.push_back(Sph_radius);
    cout << "Continue(y/n)";
    cin >> resp;
    }
    while (tolower(resp[0])=='y');

    cout << Sph_radius << endl;
}

float Sphere::area()
{
    return 4*M_PI*pow(Sph_radius, 2);
}

float Sphere::volume()
{
    return (4*M_PI*pow(Sph_radius, 3))/3;
}

Sphere.h

#ifndef SPHERE_H
#define SPHERE_H


class Sphere
{
    public:
        Sphere();
        float variable();
        float area();
        float volume();
        Sphere(float);
        double Sph_radius, Sph_SA, Sph_Vol;

    private:


};

#endif // SPHERE_H

main.cpp

    #include <iostream>
    #include "Sphere.h"
    #include <string>
    #include <cstdlib>
    #include <cmath>


using namespace std;

int main()
{
    system("CLS"); //Flush the stream
    int loop = 1;
    int choice;
    string getinput;
    bool done = false;

    while (!done)
        {
        system("CLS"); //Flush the stream
        cout << "--Menu--" << endl << endl;
        cout << "1. Add spheres" << endl;
        cout << "2. Sort sphere" << endl;
        cout << "3. Displays all" << endl;
        cout << "4. Quit" << endl;
        cin >> choice;


    if(choice < 0 || choice > 5)
        cout << "Between 1~6 please." << endl; // between 1~6
        system("pause");

    switch(choice)
        {
        case 1:// Sphere
            {
                Sphere info;
                info.variable();

                system("pause");
                break;
            }
        case 2:// Sort sphere
            {
                break;
            }
        case 3:// Displays all
            {
                Sphere info;
                cout << "test1" << endl;
                info.area();
                cout << "test2" << endl;
                system("pause");
                break;
            }


        case 4: //Quit, couldn't find any method, other than exit(0);
            {
                if(choice==4)
                {
                cout << "Thank you for using the program and See you later!";
                exit(0);
                }
            }

        }
     }
    return 0;
}

Someone help me please... I am so lost.

I'd take vector<int> list out of the Sphere class. I'd declare a vector of Spheres in main() instead and then I'd maker variable a stand alone function and use the loop to the user enter as many radius as they want and use each entry to create aother Sphere that can then be pushed onto the vector of Spheres. Then you can sort the spheres using whatever criteria you want. You compare the data members of two objects and then swap the objects themselves. You can use any data member to do the sort on and use any sort protocol you want (if you use sort() from STL algorithm header file, then you will need to overload the less than operator, or some other comparator function, for Sphere class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.