Hi ppl , i having problem caculating the total monthly expenses , anyone mind help mi out with this ?

#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
using namespace std;



int main()
{
double total;
int choice;
fstream file;
cout<<"This program is to record his / her daily expenses.\n";
cout<<"====================================================\n\n";
cout<<"\nEnter 1 to record his / her daily expenses.\n";
cout<<"Enter 2 to retrieve his / her daily expenses.\n";
cout<<"Enter a choice : ";
cin >> choice;
if(choice==1)


{

file.open("Expenses.txt",std::ofstream::out| std::ofstream::app);
char Enter ;
double total=0,amount;
while(1)
{


char description[60];
cout << "\nEnter item description: ";
cin >> description;
file<<"Item description: " << description << endl;
char Date[60];
cout << "Enter Date of purchase: ";
cin >> Date;
file<<"Date of purchase: " << Date << endl;
cout <<"Enter amount: $";
cin >>amount;
file<<"Amount: $" << amount <<endl;

total+=amount;
file<<"Daily Expenses: $" << total <<endl;
amount++;

cout <<"\nEnter E or e to end program else other key to continue :";
cin >>Enter;
if(Enter == 'E'|| Enter == 'e')
break;

}
file.close();
cout<<"\n";
cout<<"Data of daily expenses had successfully recorded , Please Verify";
cout<<"\nExpenses :$" << total <<endl;


}

if(choice==2)
{
cout<<"\nTotal monthly expenses data sheet. ";
cout<<"\nTotal monthly expenses:$"<<sum<<endl;
cout<<"\n";
ifstream file;
file.open("Expenses.txt");
string line;



while(file.good())
{
getline(file,line);
cout<<line<<endl;



}

}



return 0;
}
Last edited on
ok srry noted and thank :)
closed account (48T7M4Gy)
Please show us some sample data or give a more detailed description of what is going wrong.

Also it would be helpful if you use code tags around your code <> on the format box ----->
Last edited on
Erm basically its the retrieving data part which need to show the monthly expeneses having error , can add up the month expenses up . i think i missing some stuff

f(choice==2)
{
cout<<"\nTotal monthly expenses data sheet. ";
cout<<"\nTotal monthly expenses:$"<<sum<<endl;
cout<<"\n";
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <cstring> // <-- BAD why this one?
#include <stdlib.h>

using namespace std;

int main()
{
    double total; // <-- BAD - must initialize all variables
    int choice;
    fstream file;
    
    cout<<"This program is to record his / her daily expenses.\n";
    cout<<"====================================================\n\n";
    cout<<"\nEnter 1 to record his / her daily expenses.\n";
    cout<<"Enter 2 to retrieve his / her daily expenses.\n";
    cout<<"Enter a choice : ";
    cin >> choice;
    
    if(choice==1)
    {
        file.open("Expenses.txt",std::ofstream::out| std::ofstream::app); // BAD - see tutorial
        
        char Enter ; // BAD - silly name
        double total=0,amount; // <-- BAD - duplication
        
        while(1) // BAD <-- infinite loop
        {
            char description[60]; // <-- BAD - declare once
            cout << "\nEnter item description: ";
            cin >> description;
            
            file<<"Item description: " << description << endl;
            char Date[60]; // <-- BAD - declare once
            cout << "Enter Date of purchase: ";
            cin >> Date;
            file<<"Date of purchase: " << Date << endl;
            cout <<"Enter amount: $";
            cin >>amount;
            file<<"Amount: $" << amount <<endl;
            
            total+=amount;
            file<<"Daily Expenses: $" << total <<endl;
            amount++; //
            
            cout <<"\nEnter E or e to end program else other key to continue :";
            cin >>Enter;
            if(Enter == 'E'|| Enter == 'e')
                break;
        }

        file.close();
        cout<<"\n";
        cout<<"Data of daily expenses had successfully recorded , Please Verify";
        cout<<"\nExpenses :$" << total <<endl; 
    }
    
    if(choice==2)
    {
        cout<<"\nTotal monthly expenses data sheet. ";
        cout<<"\nTotal monthly expenses:$"<<sum<<endl; // BAD - sum not declared??
        cout<<"\n";
        ifstream file;
        file.open("Expenses.txt");
        string line;
        
        while(file.good())
        {
            getline(file,line);
            cout<<line<<endl;
        }
    }
    return 0;
}


This is a bit of a tidy up and what it shows is you don't have a plan to what you are doing. Your code is all over the place and I am not surprised you are lost.

Show us the question please.

You should also use the tutorials here on opening files and extracting data. In the first instance you should get that part working on a small program and then move on by working on sub units of your plan.

Data declarations go at the start of main, not splashed around the way you have done.

Keep your code tidy - delete blank lines unless they are there to show different parts clearly. Use white space and indentation to advantage to make it clear to read. :)
Last edited on
closed account (E0p9LyTq)
Data declarations go at the start of main, not splashed around the way you have done.


It is a matter of style, but there are reasons for declaring locals near first use:
https://isocpp.org/wiki/faq/coding-standards#declare-near-first-use
hi the question is as follow ,

1.User to enter his / her daily expenses. It should
include date, description, amount, etc.
2.Retrieve daily expenses. Display all details and
calculate expenses per month.
closed account (48T7M4Gy)
It is a matter of style, but there are reasons for declaring locals near first use:
Well nit-picked FG, now for a constructive comment perhaps?
Erm im not very strong in this , but anyone mind helping me by telling me how am i gonna sum up the total amount from different files input ? Some tell me to create a data storage for it and add it when u input new data , but i not sure how to create it . Hope u guys can help mi with it thx .
closed account (48T7M4Gy)
Well you need to start by first getting option 1 to work. Does it work? Have you created a data file and checked whether you can read it in notepad?

Once you have done that and read in 2 or 3 data records, go to step 2 and get choice 2 to just print out each record.

If you can't do that then go to the tutorials on this site and use the samples to solve any problems.

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;


int main()
{
char Enter ;
double sum=0, total,amount;
int choice;
fstream file;
cout<<"This program is to record his / her daily expenses.\n";
cout<<"====================================================\n\n";
cout<<"\nEnter 1 to record his / her daily expenses.\n";
cout<<"Enter 2 to retrieve his / her daily expenses.\n";
cout<<"Enter a choice : ";
cin >> choice;
if(choice==1)

{

file.open("Expenses.txt",std::ofstream::out| std::ofstream::app);

while(1)
{


char description[60];
cout << "\nEnter item description: ";
cin >> description;
file<<"Item description: " << description << endl;
char Date[60];
cout << "Enter Date of purchase: ";
cin >> Date;
file<<"Date of purchase: " << Date << endl;
cout <<"Enter amount: $";
cin >>amount;
file<<"Amount: $" << amount <<endl;

sum+=amount;
file<<"Daily Expenses: $" << sum <<endl;
amount++;

cout <<"\nEnter E or e to end program else other key to continue :";
cin >>Enter;
if(Enter == 'E'|| Enter == 'e')
break;

}
file.close();
cout<<"\n";
cout<<"Data of daily expenses had successfully recorded , Please Verify";
cout<<"\nExpenses :$" << sum <<endl;


}

if(choice==2)
{
cout<<"\nTotal monthly expenses data sheet. ";
cout<<"\nTotal monthly expenses:$"<<total<<endl;
cout<<"\n";
ifstream file;
file.open("Expenses.txt");
string line;



while(file.good())
{
getline(file,line);
cout<<line<<endl;



}

}



return 0;
}



ya option 1 is is working , left with option 2 , the total expenses cant add , as the file closes for first part , no data is save thats y 2nd part the monthly total expenses cant solve , i need a code or a way to save the daily expenses in the part one up so that i can use it for sum of monthly expenses . basically is the total monthly expenses calculation im with errors only
Last edited on
closed account (48T7M4Gy)
Once you have done that and read in 2 or 3 data records, go to step 2 and get choice 2 to just print out each record.

So does that part work?

BTW what is the purpose of you pasting the same stuff each time?
ok , srry again , ya the record is printed out for both . Now if i key in 30 times the data , the total expenses which is for monthly must be added up . how u gonna do that ...
closed account (48T7M4Gy)
how u gonna do that

You progressively add the relevant item as you proceed through the data set. Of course you must declared an appropriate variable at the start and initialize it.

getline wasn't a good move unless you wanted to tokenize the string. You can do it more simply by file << variable1 << variable2 etc but it's your call what u do.
Topic archived. No new replies allowed.