Storing values??

How do I store the multiple values in one variable, Or in other words, how do i put multiple calculate values in a single variable? Is it possible if I use arrays?(Can anyone show me an example codes).


I have to create a program where it will use class and
1. Add sphere(if I put Radius, it will calculate the volume and surface)
2. Sort added sphere by its volume size.
3. Display All
4. Quit (I know how to do this!)

Any better suggestion how to program this code :/?

So far this is my coding:

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
70
71
#include <iostream>
#include "Shapes.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;
                system("pause");
                break;
            }
        case 2:// Sort sphere
            {
                Sphere INFO;
                break;
            }
        case 3:// Displays all
            {

                system("pause");
                break;
            }
        case 4:// Displays all
            {

                system("pause");
                break;
            }

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

        }
     }
    return 0;
}


Shapes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef SHAPES_H
#define SHAPES_H


class Shapes
{
    public:
        Shapes();
    private:
};

class Sphere
{
    public:
        Sphere();
    private:
        double Sph_radius, Sph_SA, Sph_Vol;

};



#endif // SHAPES_H 



Shapes.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
#include <iostream>
#include "Shapes.h"
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;

Shapes::Shapes()
{
    cout<< "boo hoo" <<endl;
}


Sphere::Sphere()
{
            cout << "Radius? " << endl;
            cin >> Sph_radius;

            while(Sph_radius <= 0) {
            cout << "Cannot be negative, Try again: ";
            cin >> Sph_radius;
            }

            cout << "Surface area: " << 4*M_PI*pow(Sph_radius, 2) << endl;
            cin >> Sph_SA;

            cout << "Volume: " << (4*M_PI*pow(Sph_radius, 3))/3 << endl;
            cin >> Sph_Vol;
}

Last edited on
You have the condition of your while loop as !done, so switch done to true in order for it to evaluate false in the next test of the loop. If you ever want to exit main without reading any lines of code that follow the end of your code, just put return 0;. The program reads it as the end of main and will exit the program normally.

To answer your question, you could use arrays to store the data, but I don't know for sure how to do it with classes. I know if you use 3 value returning functions, then you will be able to put the data into arrays. I think you could also try a void function that you pass the arrays to and it will change them as it gets to them. If you want it to be in the one object, then I would try declaring the class variables as arrays and doing a display function.

If you want to be able to change the array size, then you'll have to learn about vectors, or declare the arrays to be a lot bigger than they will ever need to be.
arrays, structs, and classes are how you would store the data. i would suggest vectors because they are resizable(arrays also are in a way but its a harder concept).
Woops, yes, I have to use vectors to do make program..
Here's my other prgram that I created:

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
#include <iostream>
#include <vector>

using namespace std;

void InputList(vector<int>& l);
void DisplayList(const vector <int>& l);
void sort(vector <int>& l);
void pass(vector <int>& l);
void swap(int&x, int&y);

int main()
{
    vector<int> list;
    InputList(list);
    sort(list);
    DisplayList(list);

    void InputList(vector<int>& l)
    {
    string resp;
    int value = 0;
        do
        {
            value = ReadInt("Value?");
            l.push_back(value);
            cout << "Continue (y/n?)";
            cin >> resp;
        }
        While (tolower(resp[0])=='y');

    }
        void DisplayList(const vector<int>& l)
        {
            for(int i=0; i<l.size();i++)
            cout << l[i] << " ";
            cout << endl;
        }
        void swap (int&x, int&y)
        {
            int tmp=x;
            x=y;
            y=tmp;
        }
        bool Pass(vector<int>& l)
        {
            bool rv = true;
            for (int i=0; i<l.size()-1;i++)
            if(l[i]>l[i+l])
            {
                swap (l[i],l[i+1]);
                rv = false;
            }

        }
        return rv;
}

....Sphere value\main.cpp||In function 'int main()':|
....Sphere value\main.cpp|20|error: a function-definition is not allowed here before '{' token|
....Sphere value\main.cpp|57|error: expected '}' at end of input|
||=== Build finished: 2 errors, 0 warnings ===|

^ still getting an error(Can't fix it and don't know how :( I am still a newbie in programming!)

I have to use bubble sort method to sort size of the volumes.
Anyone know any good website to learn vectors?
declare functions as such
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>

class card{
public: 
    char type; 
    int dice, total; 
};

int randomnumber(); 
char randomletter();
void print(std::vector<card>); 
int convert(char a);  
bool sortorder(const card &a, const card &b); 

int main(){
    std::vector<card> cards; cards.resize(5);
    srand((unsigned)time(0)); bool run = true; char eord; 
    while(run){
    start: 
        std::cout << "\nWould you like some cards?(Y/N)";
        std::cin >> eord; 
        if(tolower(eord) == 'n') break; 
        
    //*******************************Numbers********************************************
    //makes dice rolls
    for(int i = 0; i < cards.size(); i++){ cards[i].dice = randomnumber();}
    //makes card types
    for(int i = 0; i < cards.size(); i++){cards[i].type = randomletter();}
        print(cards);
        std::cout << "\nWould you like to sort your cards?(Y/N)";
        std::cin >> eord; 
        if(tolower(eord) == 'n') goto start;//do not really like this  
        
    //********************************Sort**********************************************
    //sorts dice rolls
        for(int i = 0; i < cards.size();i++){ cards[i].total = cards[i].dice+convert(cards[i].type);}
        std::sort(cards.begin(), cards.end(), sortorder); 
        print(cards); 
    } 
    //********************************End***********************************************
    return 0;
}

bool sortorder(const card &a, const card &b){
    if(a.type < b.type) 
        return true; 
    if(a.type == b.type){
        if(a.dice > b.dice)
            return true;
    }
    return false; 
}

int convert(char a){
    a = tolower(a);
    if(a == 'd') return 30;
    if(a == 's') return 0;
    if(a == 'h') return 15; 
    else return NULL; 
}

void print(std::vector<card> card){
    for(int i = 0; i < card.size(); i++) std::cout << card[i].dice << " ";
    std::cout << '\n'; 
    for(int i = 0; i < card.size(); i++) std::cout << card[i].type << " "; 
}

int randomnumber(){
    return rand()%14+2;
}

char randomletter(){
    switch (rand()%3) {
        case 0:
            return 'd';
            break;
        case 1:
            return 's'; 
            break;
        case 2:
            return 'h'; 
            break;
            
        default:
            break;
    }
    return NULL; 
}
random file i found on my computer. also there is a sort function in the algorithm library which you do sort(vector.begin(),vector.end());
the template for the function is void sort ( RandomAccessIterator first, RandomAccessIterator last );
Last edited on
Topic archived. No new replies allowed.