Problem

Pages: 12
NM i got it
I want to make it so that when you spend money it tracks everything you spent, would i use a vector or an array for that?
This is ridiculous

1
2
3
4
5
void treasury(long int &M);
void game(string &PN, string &CN, long int &M, long int &HC, double &TXR, string &BLLP, string &BLLV);
void save(string &PN, string &CN, long int &M, long int &HC, double &TXR, string &BLLP, string &BLLV);
void NatManager(long int &M, long int &HC, double &TXR, string &BLLP, string &BLLV);
void MnthDeduct(long int &M, long int &HC, double &TXR);


Should i be using a class?

i still have like 30 more variables to add.
Last edited on
How would a class look with My vars. Every time i make a class someone tells me its fugly and im doing it wrong, but if i see an example with my code i'll understand it better.
Thats actually not a lot of variables.

You also want much more than one class if you wanted classes at all. The transition to classes is often a fair amount of work, for example, a budjet:
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 <vector>
#include <string>
#include <iostream>
#include <fstream>

class Entry
{
public:
  Entry(void) :title(""), amount(0) {}
  Entry(std::string title, double amount) : title(title), amount(amount) {}
  std::string title;
  double amount;

  void set(std::string title, double amount)
  {
    this->title = title;
    this->amount = amount;
  }

  void print(std::ofstream output = cout)
  {  
    output << title << ": " << amount << std::endl;
  }
};

class Budget
{
  std::string name;
  std::vector<Entry> entries;
  double initialBudget;
  double currentSpendings;

public:
  Budget(std::string name, double initial = 0) :name(name), initialBudget(initial), currentSpendings(0) {}

  bool addEntry(std::string entryName, double amount);
  void print(std::ostream output = cout);
};

bool Budget::addEntry(std::string entryName, double amount)
{
  if (currentSpendings + amount > initialBudget)
  {
    std::cout << "We can't spend " << amount << "." << std::endl;
    return false;
  }
  entries.push_back(Entry(entryName, amount);
  currentSpendings += amount;
  return true;
}

void Budget::print(std::ostream output)
{
  output << "Budget: " << name << std::endl
            "Initial Budget: " << initialBudget << std::endl
            "Current Spendings: " << currentSpendings << std::endl
            "Amount Available: " << initialBudget - currentSpendings << std::endl;
  for (std::vector<Entry>::iterator iter = entries.begin(); iter !+ entries.end(); iter++)
  {
    *iter.print(output);
  }
  output << "End of " << name << "\'s budget\n\n";
}

int main(void)
{
  Budget myBudget("Lowest0ne", 200);
  Entry temp;
  
  temp.set("Internet", 25);
  std::cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";

  temp.set("Everything else", 3000);
  std::cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";


  temp.set("Won lottery", -5000);
 std:: cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";


  temp.set("Everything else", 3000);
  std::cout << temp.title << " is " << (myBudget.addEntry(temp.title, temp.amount) ? "" << "not" ) << " okay\n";  

  myBudget.print();

  std::ofstream outFile("myBudget.txt");
  myBudget.print(outFile);
  outFile.close();

  return 0;
}


That might compile :p. The idea of classes is not to reduce the amount of variables you need, the idea is to break your code into manageable pieces.
Maybe you could use a container.
LowestOne, I appreciate the help but that code is really hard for me to understand. Can you set up the most basic class to hold my variables? You dont need to make main, just the class or classes or whatever you need to do, i can do the rest of the stuff i just need to see how the class is set up. I know how to make a class and i could actually do it but some people say getter and setter methods are evil and some dont so i dont know what to do that has me confused on what the right way is. Can you please finish this for me using whatever method you use and think is best. If you need the rest of my code let me know.

This is the actual class with all of my variables and function names in my program.

All variables will be modifyable by the player except money, the program will modify that as necessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class SetUp
{
    public:
        void treasury();
        void game();
        void save();
        void NatManager();
        void MnthDeduct();

    private:
        long int money; //Should start at 5000000000
        string Pname; //Player Name
        string Cname; //Country Name
        long int hCare; //Health Care Budget amount, should be 500000
        double taxRate; //The tax rate should be 2.5%.
};

Last edited on
bump
Just as you said, your classes are fugly. A class is it's own object, a room, a chair, a book. A book has an amount of pages, author, etc..., but it doesn't know anything about the glasses the reader wears.

The classes I made are as simple as I can make and still be anything like what you are trying to do.
This is how i would make a class, this is one i used in a previous program. Is this any better?

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
class Vars
{
    public:
        void MainProgram();
        void Names();
        void Options();
        void RNums();
        void Save();
        void Load();
        void setStrings(string PNAME, string BNAME)
        {
            pname = PNAME;
            bname = BNAME;
        }
        string getPname()
        {
            return pname;
        }
        string getBname()
        {
            return bname;
        }

        void setIntegers(int MONEY, int XP)
        {
            money = MONEY;
            xp = XP;
        }
        int getMoney()
        {
            return money;
        }
        int getXp()
        {
            return xp;
        }
    private:
        string pname;
        string bname;
        int money;
        int xp;
};
OR, i was learning about child and parent classes, i think there called abstract classes, do i make one of those?
Topic archived. No new replies allowed.
Pages: 12