Sum of specific integers in .txt file

Hi!
I haven't done a lot of coding before, but now I'm working on this budget/accounting-program. My first goal is to develop a program were the user puts in an amount of money they have spent and which category it's in (food, rent, clothes, etc). This information will then be stored in a .txt file. An example of how I want it to look like:

Food 500
Rent 1000
Clothes 300

Where the number represents the sum of all the amounts of money spent in that category that month. And this is where I'm stuck - how do I sum all these inputs (and ofcourse keep the categories divided)?

The way I work: I start reading from the file, and loads the whole file into a vector, using push_back. When the user is done with their input, the vector is then loaded back to the file. The category is a string, and the amount and the sum is integers.

Any tips? Is it better to change the file directly, or use arrays instead of a vector?
Last edited on
Show us your code so far ...
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
//main.cpp:
#include <iostream>
#include <string>
#include "Month.h"
#include "MonthManager.h"

MonthManager monthManager;
Month month;

using namespace std; 

void menu();

int main ()
{
	menu();
}

void menu()
{
	int n = 0;
	while (n < 3) //Just to run it some times
	{
		cout << "1. Expence " << endl << "2. Income" << endl;
		int choice; 
		cin >> choice; 

		if (choice == 1)
		{
			int amount, sum; string category;
			cout << "What category is the expence in?" << endl; 
			cin >> category; 
			cout << "How much did it cost?" << endl; 
			cin >> amount;
			//Some code for declearing the sum is needed here
			monthManager.addExpense(category, amount, sum); 
		}

		if (choice == 2)
			//Not started yet
		
		n++;
	}

}

//Month.h:
#pragma once
#include <string>

class Month
{
public:
	Month(void);
	~Month(void);

	Month(std::string category, int amount, int sum);
	std::string getCategory();
	int getAmount();
	int getSum();

private: 
	std::string _category; 
	int _amount;
	int _sum;
};

//Month.cpp:
#include "Month.h"
#include "MonthManager.h"
#include <string>
#include <vector>

using namespace std;

MonthManager monthManager;

Month::Month(void)
{
	_category = "Hi";
	_amount = 0; 
	_sum = 0; 
}


Month::~Month(void)
{
}

Month::Month(string category, int amount, int sum)
{
	_category = category; 
	_amount = amount; 
	_sum = sum;
}


string Month::getCategory()
{
	return _category;
}

int Month::getAmount()
{
	return _amount;
}

int Month::getSum()
{
return _sum;
}

//MonthManager.h:
#pragma once
#include <vector>
#include <string>
#include "Month.h"

class MonthManager
{
public:
	MonthManager(void);
	~MonthManager(void);
	void addExpense (std::string category, int amount, int sum);


private: 
	void readFromFile();
	void saveToFile();
	std::vector<Month> _tmpMonth;
};



//MonthManager.cpp: 
#include "MonthManager.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std; 


MonthManager::MonthManager(void)
{
	readFromFile();
}


MonthManager::~MonthManager(void)
{
}

void MonthManager::readFromFile()					
{								
}

void MonthManager::saveToFile()			//Haven't done these two functions yet				
{
}

void MonthManager::addExpense(string category, int amount, int sum)	
{
	readFromFile();
	for (int i = 0; i < _tmpMonth.size(); i++)
	{
		if (_tmpMonth[i].getCategory() == category)
		{
			sum += amount;
			Month temp(category, amount, sum);
			_tmpMonth.push_back(temp);
		}


	}

	saveToFile();
}



Last edited on
Surprised no one has responded yet as it doesn't seem a hard problem in itself. Unfortunately I am in the beginner c++ category and know nothing yet about vectors or push_back. I have done similar things in plain old c but that is not what you need. I don't even know how to deal with multiple files yet.
Last edited on
Anyone? :)
Topic archived. No new replies allowed.