// s2.cpp : Defines the entry point for the console application.
// Project Stage 2.cpp : Defines the entry point for the console application.
/* Algorithm
Menu()
Choose option
Create a new expense
Enter the initial amount of money in bank or to add money to bank from paycheck
Enter the new expense
Enter the expense category
Input whether expense is recurring or not*
Input whether expense is saved for or not
calculate balance after expense
display financial status*/
#include <iostream>
#include <cstring>
#include <cassert>
#include <fstream>
using std::strncpy;
using std::memset;
using namespace std;
struct node
{
char *name;//dynamic array for name of expense
char *cat;//category of expense
float cost;//cost of expense
void create();
node* next;
node();//node constructor
};
class expense
{
private:
node *head;
float balance;
float cash;
public:
expense();//constructor
~expense();//destructor
void create();//function to create expense
void display();//display function for expenses
void read();
void write();
void deposit();
void add();//add expenses total
void status();
};
expense::expense()
{
head = NULL;
}
expense::~expense()
{
node* current=NULL;
while(head)
{
current = head->next;
if(head->name)
delete [] head-> name;
if(head->cat)
delete [] head->cat;
head->cost = 0.0;
delete head;
head = current;
}
}
void expense::deposit()
{
cout<<"You have chosen the option to deposit money into your revolving account."<<endl;
cout<<" "<<endl;
cout<<"Enter the amount you would like to deposit: $ ";
cin>>cash;
cout<<" "<<endl;
cout<<"To see how much you have, and if you can afford purchase choose option financial status"<<endl;
}
void expense::add()//function that adds all expenses
{
//code for adding my cost
//return total
//total variable for expense totals
}
void expense::status() //function that tells you whether or not you can afford a purchase
{
float price=0;
cout<<"What is the price of your intended purchase: $ "<<endl;
cin>>price;
//if price greater than balance then can't afford new purchase
//if price less than balance, then you can afford new purchase
}
void expense::display() //this function displays the expenses you have just created as well as those read in from external file
{
node*current = head;
while(current)
{
cout<<" "<<endl;
cout<<current->name<<" "<<current->cat<<" $"<<current->cost<<'\n'; //display format same as the format of data file
cout<<" "<<endl;
current = current->next;
}
}
void expense::read()
{
static const size_t buf_size = 200;
ifstream infile;
infile.open("expense.txt");
if (infile.fail()) {
cout<<"Failed to open the file..." << endl;
return;
}
char aray[buf_size];
node*current = NULL;
memset( aray, 0, buf_size ); // Set array to zeroes.
infile.getline(aray,buf_size,':');
int temp=0;
while(!infile.eof())
{
current = head; //Initialize values
//head = NULL;
//head = new node;
//head->name = NULL;
//head->next = NULL;
//head->cat = NULL;
//head->cost=0.0;
infile.get();
// head->next=current;
head->name = new char[strlen(aray)+1];
strncpy(head->name, aray, buf_size-1); // string length
infile.getline(aray,buf_size,':');
infile.get(); // eat colon
head->cat = new char[strlen(aray)+1];
strncpy(head->cat, aray, (buf_size-1));
infile.get(); infile.get(aray, (buf_size), ':'); //eat the colon
//infile.get();
infile >> head->cost;
infile.get();//eat the '/n'
infile.getline(aray,buf_size,'\n');//get cost
cout<<aray<<endl;
head->cost=temp;
infile.get();
infile.getline(aray,buf_size,':');
assert(head);
//cout << head->name << ':';
//cout << head->cat << ':';
//cout << head->cost << '\n';
cout<<"hi"<<endl;
}
infile.close();
}
void expense::write()
{
ofstream outfile;
outfile.open("expense.txt");
node *current = head;
if(!outfile.is_open())
cout<<"couldn't open the file"<<endl;
while(current!=NULL)
{
outfile<<current->name<<":"<<current->cat<<":"<<current->cost<<'\n';
cout<<current->name<<":"<<current->cat<<":"<<current->cost<<'\n';
current=current->next;
}
outfile.close();
}
/*class expense_track::expense //access to class expense
{
public:
expenseTrack();
void displayallexpenes();
void expensetotalshow();
int *search (char cat[]);
void deletexpense();
private:
int numexpenses;
};
expenseTrack::expenseTrack()
{
numexpenses=0;//data member
}*/
//member functions to describe expense class
void node::create()
{
char temp[201];
char temp2[201];
cout<<"Creating a new expense....."<<endl;
cout<<"Enter the name of the expense: ";
cin>>temp;
name=new char[strlen(temp)+1];
strcpy(name,temp);
cout<<"Enter the expense type: (MONTHLY/MISC/TAX/LUXURY) ";
cin>>temp2;
cat = new char[strlen(temp2)+1];
strcpy(cat,temp2);
cout<<"Enter the cost of the expense: $";
cin>>cost;
}
void expense::create()
{
node*current = head;
head= new node;
head->create();
head-> next = current;
}
int main ()
{
expense exp;
int choice=0;
int i;i=0;
cout<<"Welcome to TONDRE'S BUDGET ANALYZER Program"<<endl;
cout<<"*******************************************"<<endl;
cout<<" "<<endl;
while(choice!=5)
{
cout<<"Please select from one of the following options"<<endl;
cout<<" "<<endl;
cout<<"1. Deposit money into your account"<<endl;
cout<<" "<<endl;
cout<<"2. Create an expense"<<endl;
cout<<" "<<endl;
cout<<"3. Display the expenses you have entered"<<endl;
cout<<" "<<endl;
cout<<"4. Save these expenses to an external file"<<endl;
cout<<" "<<endl;
cout<<"5. Choose to quit."<<endl;
cout<<" "<<endl;
cout<<"6. Choose to see stored expenses on external file."<<endl;
cout<<" "<<endl;
cout<<"Type in a choice."<<endl;
cout<<" "<<endl;
cin>>choice;
cout<<" "<<endl;
switch (choice)
{
case 1:
cout<<"You chose option 1. You want to deposit some money into your account. "<<endl;
//exp.income();
break;
case 2:
cout<<"You chose option 2. Create a new expense."<<endl;
exp.create();
break;
case 3:
cout<<"You chose option 3. Review and display expenses list."<<endl;
// exp.read();
exp.display();
break;
case 4:
cout<<"You chose option 4.Write created expenses to file"<<endl;
exp.write();
case 5:
break;
case 6:
cout<<"You chose to see your stored information."<<endl;
exp.read();
exp.display();
break;
default:
cout<<"You have made an illegal choice."<<endl;
}
}
char reply;
cout << "Press q (or any other key) followed by 'Enter' to quit: ";
cin >> reply;
return 0;
}
I can't do option 6. Reading in from external file. I get seg errors