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)
{
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
#include <iostream>
#include <fstream>
#include <cstring> // <-- BAD why this one?
#include <stdlib.h>
usingnamespace 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. :)
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
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.
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 .
#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)
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
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 ...
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.