Class help

Pages: 12
What am i doing wrong here? Im trying to make a class for all of my variables so its easier to manage but am i doing it right?

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//void treasury(long int &M);
//void game(string &PN, string &CN, long int &M, long int &HC, double &TXR);
//void save(string &PN, string &CN, long int &M, long int &HC, double &TXR);
//void NatManager(long int &M, long int &HC, double &TXR);
//void MnthDeduct(long int &M, long int &HC, double &TXR);


class SetUp
{
    private:
        long int money;
        string Pname;
        string Cname;
        long int hCare;
        double taxRate;

    public:
        SetUp(long int M, string PN, string CN, long int HC, double TXR):
            money(M), Pname(PN), Cname(CN), hCare(HC), taxRate(TXR){}
};


void treasury();
void game();
void save();
void NatManager();
void MnthDeduct();

int main()
{
    int choice;
    //long int money = 1000000000;
    //string Pname;
    //string Cname;
    //long int hCare = 24;
    //double taxRate = 35;

    SetUp SO;

    cout << "1) New" << endl;
    cout << "2) Load" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            {

                cin.sync();
                cout << "Welcome please enter your name" << endl;
                getline(cin, Pname);
                cout << "\n";
                cin.sync();
                cout << "Thank you " << Pname << " now please enter your countries name." << endl;
                getline(cin, Cname);

                cout << "Welcome " << Pname << " to " << Cname << endl;
                cout << "Press ENTER to continue\n" << endl;
                cin.get();

                save(Pname, Cname, money, hCare, taxRate);
            }
            break;
        case 2:
            {
                ifstream file("President.txt");

                getline(file, Pname);
                getline(file, Cname);
                file >> money;
                file >> hCare;
                file >> taxRate;

                game(Pname,Cname,money,hCare,taxRate);
            }
            break;
        default:
            cout << "Error" << endl;
    }
}

void game()
{
    int choice;

    cout << "What will you do?\n" << endl;

    cout << "1) Go to Treasury" << endl;
    cout << "2) Go to Nation Manager" << endl;
    cout << "3) View monthly deductions and earnings" << endl;
    cout << "4) Save" << endl;
    cin >> choice;

        switch(choice)
        {
            case 1:
                treasury();
                break;
            case 2:
                NatManager();
                break;
            case 3:
                MnthDeduct();
                break;
            case 4:
                save();
                break;
        }
}

void treasury()
{
    cout << "NATIONAL TREASURY\n" << endl;

    cout << "Current money in treasury: " << M << endl;
}

void save()
{
    ofstream file;
    file.open("President.txt");

    file << PN << endl;
    file << CN << endl;
    file << M << endl;
    file << HC << endl;
    file << TXR << endl;

    file.close();

    return game();
}

void NatManager()
{
    int choice;
    long int healC = 0;
    double taxR = 0;

    cout << "What do you want to do here?\n" << endl;

    cout << "1) Modify Healthcare" << endl;
    cout << "2) Modify Taxes" << endl;
    cout << "3) Pass/veto bills" << endl;
    cin >> choice;
    cout << "\n";

        switch(choice)
        {
            case 1:
                cout << "MODIFY HEALTHCARE\n" << endl;

                cout << "Current Healthcare budget: $" << HC << endl;
                cout << "What would you like to change the budget to?" << endl;
                cout << "Current Government Budget: $" << M << endl;
                cin >> healC;

                if(healC < M)
                {
                    HC = healC;

                    cout << "\n";
                    cout << "Ok current Healthcare budget is set to: $" << HC << endl;
                    cout << "Current National budget: $" << M - healC << endl;
                }
                else if(healC > M)
                {
                    cout << "Thats more money than you have!" << endl;
                }
                break;
            case 2:
                cout << "MODIFY TAXES\n" << endl;

                cout << "Current Tax Rate: %" << TXR << endl;
                cout << "What would you like to change the Tax Rate to?" << endl;
                cin >> taxR;

                cout << "\n";

                TXR = taxR;

                cout << "Ok the current tax rate is now: %" << TXR << endl;
                break;
            case 3:
                cout << "PASS/VETO BILLS\n" << endl;

                cout << "CURRENT BILL LIST\n" << endl;

                cout << "" << endl;
        }
}

void MnthDeduct()
{
    cout << "MONTHLY DEDUCTIONS AND EARNINGS\n" << endl;

    cout << "DEDUCTIONS\n" << endl;

    cout << "Current Money: $" << M << endl;
    cout << "Current Taxes: %" << TXR << endl;
    cout << "Current Health Care Budget: $" << HC << endl;
}


I learned a bunch of stuff about classes but GD every time i make a class someone tells me im doing it wrong, so can someone please fix my class for me using simple code a beginner would understand so i can see how its supposed to look. I'm pretty sure i'll know how it will function once i see it. You dont need to fix everything, just please create the class using my vars and function prototypes, i know how to do everything else. thanks.
Last edited on
Making a class is like making a sandwich. What order do you like to put things in? Do you like it toasted? Swiss or cheddar cheese?

The only problem here is that there are errors. Is you're question how to rewrite and use a class properly? Or that you're getting errors and that you don't know why?
Well, i learn about classes from articles and videos, then people tell me im not doing them right or somethings wrong or i should do this or that or whatever. I want to know how to write a class that most people will be fine with, cause i am not doing a good job. Reading articles on this site and stuff dont help. Its like reading jibberish. I like people to help me and explain stuff because its 80% easier for me to understand it. But yeah i just want to know how my class will look with my variables and all that other stuff. I dont need everything else fixed, i can do that I just need the class.
Well the way you're using your variables I would say that the class you need to design won't be just variables. Instead of calling it "setUp" call it something like "Person" or "Player" because this is what the variables represent.

This is how I usually setup a class:

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
class Person{
public:
    double money;
    double hCare;
    string pName;
    string cName;
    double taxRate;

    Person(string pName, string cName, double money, double taxRate, double hCare)
    {
        m_pName = pName;
        m_cName = cName;
        m_money = money;
        m_hCare = hCare;
        m_taxRate = taxRate;
    }

    void save()
    {
        ofstream file;
        file.open("President.txt");

        file << m_pName << endl;
        file << m_cName << endl;
        file << m_money << endl;
        file << m_hCare << endl;
        file << m_taxRate << endl;

        file.close();
    }
};

int main()
{
    vector<Person> people;

    string name, cName;
    double money = 100, taxRate = .20, hCare = 1.0;

    cout << "Name: ";
    cin >> name;
    cout << "Country: ";
    cin >> cName;

    person p1(name, cName, money, taxRate, hCare);
    p1.save();
    people.push_back(p1);

    //now you have Person one loaded in memory and saved in a file
    cout << "Hello " << people[0].m_pName << "." << endl;
    //or
    cout << "Hello" << p1.m_pName << "." << endl;

    return 0;
}


That's just a very basic way of using a class to represent data and to have simple functionality for it. If you wanted the variables to be private you'd need setters/getters for those variables.

The problem is that there are tons of ways to create stuff like this and you'll never fully learn all the ways. You just develop a method that's comfortable and works well. If you're ever coding on large projects with multiple people be sure to make many comments within the class. People know what a type of "int" is... But they might not know how you built the type of "Person"
I see, thats pretty much like how i was doing it before. Ok well i'll do that, but i have a question, whats the vector for i dont understand that?
vectors are dynamic arrays that come as a tool to the c++ language. It is a part of the Standard Template Library or STL. Basically think of a int[] array but is dynamic and comes with built in functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int intArray[5];
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
intArray[4] = 5;
//can't add anymore

vector<int> intVector;
intVector.push_back(1);
intVector.push_back(2);
intVector.push_back(3);
intVector.push_back(4);
intVector.push_back(5);
//can add more

for(int i = 0; i < 5; i++)
cout << intArray[i] << endl;

for(int i = 0; i < intVector.size(); i++)
cout << intVector[i] << endl;


The example shows an array of ints VS. a vector of ints. Same functionality but different ways of implementing them.

Read up on vectors. Their awesome.
http://www.cplusplus.com/reference/stl/vector/

Also read up on STLs. They make your life a lot easier when used with STL algorithms.
http://www.cplusplus.com/reference/stl/
Ok, i know a little about vectors and what they do and why there useful but i have never used them in a program myself. now i have a question about the code

How would i endlessly keep adding stuff to the vector in my compiled program? i have seen it done and i think i might know how but its a little fuzzy.
What do you mean by "endlessly" ? Usually, you add items to a vector by using push_back(). The vector resizes itself as you add items to it. You can add as many items as you want (well, at least until you run out of memory...).
In reference to your code, this will add 5 people to the vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    
int people  =0;
while(people < 5)
{
    cout << "Name: ";
    cin >> name;
    cout << "Country: ";
    cin >> cName;

    person p1(name, cName, money, taxRate, hCare);
    p1.save();
    people.push_back(p1);

    //now you have Person one loaded in memory and saved in a file
    cout << "Hello " << people[0].m_pName << "." << endl;
    //or
    cout << "Hello" << p1.m_pName << "." << endl;
    people++
}
Ok, well im trying to make a vector that lets you keep entering names until you get to 30 names, but im not sure how to do it. I know i would use a for loop but imn not sure about the rest how would that look? Would i need to make an array or vector for all the different strings?

Last edited on
1
2
3
4
5
6
7
8
std::vector<std::string> svec;
string s;
for(int i = 0; i < 30; ++i)
{
    std::cout << "Please enter string number " << i << " :" << std::endl;
    getline(cin, s);
    svec.push_back(s); 
}
Last edited on
ok, i understand all of that thank you. now how would i output all f the things i entered at the end of the vector?
std::cout << svec[0] << std::endl;

This outputs the first string in the vector. You can use a loop to output every string in a vector.
Like this?

1
2
3
4
    for(int o = 0; o < 30; o++)
    {
        cout << svec[0] << endl;
    }


It just gives me the first 30 times I know im doing something wrong but what?
cout << svec[0] << endl;
You're outputting the first element every time here. Using "o" as a variable name here, may seem confusing too.
So i have to manually type in svec[1], svec[2] etc?
Of course not, what's the use of the loop then ?

Here...

1
2
3
4
for(int i = 0; i < 30; ++i)
{
     std::cout << svec[i] << std::endl;
}
Got it, ok i get it now. awesome thanks.
Ok so i made it so my vector will allow you to enter how many variables you want to enter and i entered 2 but it only let me enter one? I did num++ and it fixed the problem but why did it do that? why did it only let me enter one name when i specifically told it i wanted 2? This code compiles fine btw theres no errors.

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

using namespace std;

int main()
{
    vector<string> svec;
    string s;
    int num = 0;

    cout << "How many names will you be entering?" << endl;
    cout << "Amount of names: ";
    cin >> num;

    num++;

    for(int i = 0; i < num; ++i)
    {
        cout << i << " ";
        getline(cin, s);
        svec.push_back(s);
    }

    cout << "\n";

    for(int i = 0; i < 2; i++)
    {
        cout << svec[i] << endl;
    }
}
Last edited on
Pages: 12