changing data every time the program is executed

please help me with this...
i want to create a expense manager. my program is such that i enter new entry. but along with that, i also want to show the total expense. all the entries that i add, are stored in a text file.
is there any way to keep on changing the 'total expense' every time the program is executed????
can we keep on cumulating the individual expense every time an entry is made and display it in text file???
is there any way to keep on changing the 'total expense' every time the program is executed????
Yes
can we keep on cumulating the individual expense every time an entry is made and display it in text file???
Sure

How easy/complicated it is depends on the format of your entries
Hi , what code have you tried for this .. ?
i have just started coding...not so good...there might be some better method of doing what i have done...but this was thee code that i had written:-



#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
int amount;
char string[256], date[12];

cout<<"Enter Date:- "<<endl;
cin.getline ( date, 12, '\n' );

cout<<"Enter details in short:- "<<endl;
cin.getline ( string, 256, '\n' );

cout<<"Enter Amount Spent (in Rs.):- "<<endl;
cin>>amount;


cin.get();

ofstream myfile;
myfile.open ("expense.txt", ios::app);
myfile<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl<<endl;
myfile<<"::: NEW EXPENSE ENTRY :::"<<endl;
myfile<<"Date:- "<<date<<endl;
myfile<<"Amount:- "<<amount<<endl;
myfile<<"Expense Description:- "<<string<<endl;
myfile.close();
return 0;

}
i am quite new to coding ...so i don't know what logic or command i can use for my problem..and it's not any of my school/college homewrk or something...so please help me solve my problem.
if you'll think i should change any part of my program so that i can get my desired output..then please do tell...can you'll help me ??? thanks for replying bluecoder an coder777
Last edited on
is there any way to keep on changing the 'total expense' every time the program is executed????

Although I did not get how you intend to do it you can use rand() to get a random value. To get different value at every program execution use srand() of course (otherwise the series of random number generated would be the same).
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
what i wish to do is:
before 1st entry, the total expense/sum must be zero.
after 1st entry, the sum is total of 1st entry.
when 2nd entry is entered, the sum must become 1st entry+2nd entry.
on 3rd entry, the sum must include 1st, nd and 3rd...and so on...
because the program must be executed differently for ading a new entry everytime, where do i keep the cumulative sum everytime the program ends so that the new value of sum can be used next time the program is executed.
Last edited on
while working with files, how can i take data stored in a file as input for my variable???
is it possible???
if it is possible, then i store the data in a separate file and keep overwriting it using the data stored.
so the new file will always contain my total expense.

so if a .txt file contains just numbers, then can that number be stored in a variable the way data is stored into a variable while using "cin"???
hi,

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
#include<iostream>
#include<conio.h>
#include <string> 
#include<fstream>
using namespace std;
int main()
{
	string str_result ; 
	float sum  =0  ,  value = 0 ; 
	char ch = 'Y'; 
	ifstream file("account.txt");
	while(getline(file , str_result))
	{
		sum = atof(str_result);
	}; 
	file.close(); 
	do
	{
		cout<<"\n Enter the value of the amount ";
		cin>>value ; 
		sum += value ; 
		cout<<"\n Do you want to continue ";
		cin>>ch; 
	}	while (ch != 'n');
	ofstream ofile( test.txt );
	ofile<<<sum; 
	ofile.close();
	move( test.txt . account.txt ); //renaming the test.txt to account.txt 
	

	
}


I have not checked this code please my apologies if there is any error .
hey...thanks bluecoder...with a little reference and your program..i got my problem solved...thanks everybody for replying...
thanks
aslan93
Topic archived. No new replies allowed.