Using vector and class?

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. 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 infloat 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
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
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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
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
#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...
Last edited on
Inside your switch, case 1 and 3, you are declaring a new Sphere each time. When the switch statement ends your Spheres are losing scope. Although you reference them both by the same name (info) they are different objects.

Inside Sphere::variable() you are not returning a value even though the function declaration says you should be.

Sphere::area() is returning a value, but you are doing nothing with it. (inside case 3).


Apart from the oversight in Sphere::variable(), your class seems pretty sound to me. The only real issue is the implementation inside Main.cpp (IE: the scope of your Sphere info, and not actually telling the program to print info.area() to the screen)

Hopefully this should help.
You have Sphere(float); in Sphere.h but you don't have it in Sphere.cpp, and you do not use it nowhere.
vector list exists only in function variable(), but not in class. And you do not do anything with it. So you do not need it.
You do not use this variables to Sph_SA, Sph_Vol so you do not need them.
Variable double Sph_radius should be in private section.
exit(0); you can change to return 0; you will have two return 0;
And the program will also gotta able to do Displaying all the values that users put(Radius, volume and Surface)

For this you need 3 vectors. vector for Radius, vector for volume and vector for Surface.

Your functions
1
2
float area();
float volume();

should calculate area and volume, and put them in they vectors.
Topic archived. No new replies allowed.