| | |
Help me please with reading in linked list txt file
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
// 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
Of course, you got memory access exception: initial head == 0 but you are trying to assign new node pointer via this null pointer:
Regrettably you did not set the language specifier into the code tag so no line numbers in your snippet. Next time use code tag properly:
[code=cpluplus]
source
[/code]
You must set the 1st node pointer into the head member (you have an empty list at this moment).
Add the code yourself then come back (there are lots of other defects in the code
).
C++ Syntax (Toggle Plain Text)
head->name = new char ...
[code=cpluplus]
source
[/code]
You must set the 1st node pointer into the head member (you have an empty list at this moment).
Add the code yourself then come back (there are lots of other defects in the code
). •
•
•
•
sorry i don't understand. you mean i should set head= new node;//i already did. doesn't that set it?
You may also like to see this: http://www.dreamincode.net/forums/showtopic10157.htm
Just scroll down and have a look at the linked list implementation. I thought that this approach could save a load of (future) hassles for you.
Hope this helped!
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
I know my code isn't cleaned up yet, but i still need to figure out how i am doing my read function wrong.
I have this code for my node, class and member functions
if i can at least understand what i'm doing wrong in these snippes, i'll be able to clean up my code and piece everything together.
i'm getting <bad ptr> errors and seg errors nomatter what i try.
i'm trying to read in from txt file to dynamic array and have these as a linked list. i dnt know how to set my initial node if i have an empty txt file and how to add node at head if i have a txt file with some data.
i've been reading a lot of tutorials but none of them have been helpful for my scenario. they all deal with int data and my data is of char type dynamically allocated and read in from a file.
I have this code for my node, class and member functions
C++ Syntax (Toggle Plain Text)
struct node { char *name;//dynamic array for name of expense char *cat;//category of expense float cost;//cost of expense void create(); node* next; }; class expense { private: node *head; public: expense();//constructor ~expense();//destructor void create();//function to create expense void display();//display function for expenses void read(); void write(); //void add();//add expenses total }; expense::expense() { head = NULL; head->cost = 0.0; } expense::~expense() { node* current=NULL; while(head) { current = head->next; if(head->name) delete [] head-> name; if(head->cat) delete [] head->cat; delete head; head = current; } } //void expense::add() //function to add all my expenses. ie. sum of cost from file void expense::display() { node*current = head; while(current) { cout<<" "<<endl; cout<<current->name<<" "<<current->cat<<" $"<<current->cost<<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; 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.getline(aray,buf_size,':'); infile.get(); infile >> head->cost; infile.get();//eat the '/n' //outfile.getline(aray,buf_size,'\n');//get cost sort of //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'; } 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(); }
i'm getting <bad ptr> errors and seg errors nomatter what i try.
i'm trying to read in from txt file to dynamic array and have these as a linked list. i dnt know how to set my initial node if i have an empty txt file and how to add node at head if i have a txt file with some data.
i've been reading a lot of tutorials but none of them have been helpful for my scenario. they all deal with int data and my data is of char type dynamically allocated and read in from a file.
>i've been reading a lot of tutorials but none of them have been helpful for my scenario.
Without any doubt you didn't understand those tutorials. You have a classic linked list building scenario.
>...they all deal with int data and my data is of char type dynamically allocated and read in from a file.
It does not matter where is your data source. A list does not know any files. It is of no importance what's the node data type. A list node essence is the link to the next node - that's all.
To add a new node:
*) That's why a good list implementation maintains not only head but also tail pointer! Otherwise you must traverse a list from the head upto the last node on every add node operation.
It seems you don't understand a pointer concept. Look at your code (lines 25-29):
That's terribly badly!
The 1st statement says: head is a null pointer, it points to nowhere.
The 2nd statement: OK, let this nowhere gets zero to its cost member!
Better start from the beginning, re-read a wonderful tutorial on pointers:
http://eternallyconfuzzled.com/tuts/..._pointers.aspx
It seems without that you can't understand our good advices now. Moreover, that's why you didn't understand a lot of tutorials...
Without any doubt you didn't understand those tutorials. You have a classic linked list building scenario.
>...they all deal with int data and my data is of char type dynamically allocated and read in from a file.
It does not matter where is your data source. A list does not know any files. It is of no importance what's the node data type. A list node essence is the link to the next node - that's all.
To add a new node:
C++ Syntax (Toggle Plain Text)
allocate a new node, fill its data fields, set next member to NULL. if a list is empty (head == NULL) set head to the new node; all done else find a pointer to the last list node *) set this node.next to the new node endif
It seems you don't understand a pointer concept. Look at your code (lines 25-29):
C++ Syntax (Toggle Plain Text)
expense::expense() { head = NULL; head->cost = 0.0; }
The 1st statement says: head is a null pointer, it points to nowhere.
The 2nd statement: OK, let this nowhere gets zero to its cost member!
Better start from the beginning, re-read a wonderful tutorial on pointers:
http://eternallyconfuzzled.com/tuts/..._pointers.aspx
It seems without that you can't understand our good advices now. Moreover, that's why you didn't understand a lot of tutorials...
Last edited by ArkM; May 19th, 2009 at 6:05 am.
•
•
•
•
i've been reading a lot of tutorials but none of them have been helpful for my scenario. they all deal with int data and my data is of char type dynamically allocated and read in from a file.
It also sounds like you haven't understood the concept of pointers, and I strongly recommend you going through the tutorials that ArkM provided in his post.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
I have went through my code and re-did it a bit. Thank you for the help so far. Now I want to know, how can I write the code for my read function, because the one I have doesn't seem to work. I want to be able to call that fucntion before my display function in my list_manager, so that whatever i do to my list, its done, to the expenses i already have stored on file.
here's my updated code
[code = cplusplus]
// expenses linked list.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using std::memset;
using namespace std;
//linked list node creation
struct node
{
char name[20]; //dynamic allocation for name
char cat[10]; //dynamic allocation for category
float cost; //$ amount
node *next; //links to next node
};
node* head=NULL;
node* current; //used to traverse through list
int option = 0;
void add_expense_at_end()
{
node *temp, *temp2; //temp pointers
//char tmp[201]; char tmp2[201];
// reserve space for new node and fill it with data
temp = new node;
cout<<"Creating an expense..."<<endl;
cout<<endl;
cout<<"Enter the name of the expense(characters no space!): ";
cin>>temp->name;
cout<<"Enter the category description(characters no space!): (MONTHLY, MISC, RENT, PAYMNT, FUN) ";
cin>>temp->cat;
cout<<"What's the cost of this expense?(numbers only) $";
cin>>temp->cost;
temp->next= NULL; //end of node
//link to node
if(head==NULL)
{
head=temp;
current=head;
}
else //list is not empty
{
temp2= head;
while(temp2 -> next != NULL)
{
temp2 = temp2 -> next; //move to next link
}
temp2 -> next = temp;
}
}
void display_list()
{
node *temp;
temp = head;
cout<<endl;
if (temp==NULL)
cout<<"Empty list"<<endl;
else
{
while(temp!=NULL) //display details for what temp points too
{
cout<<"Name: "<<temp->name<<" ";
cout<<"Category: "<<temp->cat<<" ";
cout<<"Cost: $"<<temp->cost<<" ";
if(temp==current)
cout<<" <--Current node";
cout<<endl;
temp = temp->next;
}
cout<<"End of expenses!"<<endl;
}
}
void delete_start_expense() //delete function for removing an expense from list
{
node *temp;
temp = head;
head = head->next;
delete temp;
}
void delete_end_expense()
{
node *temp1, *temp2;
if (head == NULL)
cout<<"The list is empty!"<<endl;
else
{
temp1=head;
if(temp1->next==NULL)
{
delete temp1;
head = NULL;
}
else
{
while(temp1->next !=NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
}
}
void move_current_on()
{
if(current->next==NULL)
cout<<"This is the last expense item."<<endl;
else
current = current->next;
}
void move_current_back()
{
if(current==head)
cout<<"This is the first expense!"<<endl;
else{
node *tail; //pointer declaration
tail = head;
while(tail->next != current)
{
tail = tail->next;
}
current = tail;
}
}
void linked_list() //linked list manager function
{
head=NULL;
do
{
cout<<endl;
cout<<"Welcome to the linked-list manager function!"<<endl;
display_list();
cout<<endl;
cout<<"Please select an option: "<<endl;
cout<<endl;
cout<<"0. Exit the manager (*Save to external file before exiting program to store list)"<<endl;
cout<<endl;
cout<<"1. Add an expense to the end of the list."<<endl;
cout<<endl;
cout<<"2. Delete first expense from the list."<<endl;
cout<<endl;
cout<<"3. Delete last expense from the list."<<endl;
cout<<endl;
cout<<"4. Traverse to next expense by one node."<<endl;
cout<<endl;
cout<<"5. Traverse the current expense back by one node."<<endl;
cout<<endl;
cout<<" Enter choice number, followed by enter: ";
cin>>option;
cout<<endl;
switch (option)
{
case 1: add_expense_at_end();break;
case 2: delete_start_expense();break;
case 3: delete_end_expense();break;
case 4: move_current_on();break;
case 5: move_current_back();break;
}
}
while (option !=0);
}
void write() //function for writing to external data file
{
ofstream outfile;
outfile.open("mylist.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; //link to next expense
}
outfile.close();
}
void read() //read in function
{
static const size_t buf_size = 200; //string copy size limit
ifstream infile;
infile.open("mylist.txt");
// infile.open();
if(infile.fail())
{
cout<<"Failed to open file..."<<endl;
return;
}
char aray[buf_size];
node *current= NULL;
memset (aray, 0, buf_size);
infile.getline(aray, buf_size,'~');
while(!infile.eof())
{
current = head;
head = new node;
head->next = current;
head->name; //= new char [strlen(aray)+1];
//strncpy(head->name, aray, buf_size-1);
infile.getline(aray, buf_size, '~');infile.get(); //eat delimeter
head->cat; //= new char[strlen(aray)+1];
//strncpy(head->cat, aray, buf_size-1);
infile.getline(aray,buf_size,'~');infile.get(); //eat delimeter
infile>>head->cost; infile.get(); //eat the newline
cout<<aray<<endl;
}
// infile.close;
}
class income
{
private:
float balance;
float cash;
public:
income();
void deposit();
void display_income();
void withdrawal();
void write();
};
income::income()
{
balance = 0.0;
cash = 0.0;
}
void deposit ()
{
cout<<"You chose to add some money into your account!"<<endl;
cout<<endl;
cout<"Enter the cash amount you have to add: $";
cin>>cash;
}
void income::write()
{
ofstream out;
out.open("money.txt");
if(!out.is_open())
cout<<"Couldn't open the file..."<<endl;
out<<balance<<'\n';
out.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
// 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. Choose to go to expense list manager"<<endl;
cout<<endl;
cout<<"3. Choose to save to external file before quitting program"<<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. Open expense list manager."<<endl;
linked_list();
break;
case 3:
cout<<"You chose option 3. Save expense to external file."<<endl;
write();
break;
case 4:
cout<<"You chose option 4.Write created expenses to file"<<endl;
case 5:
break;
case 6:
cout<<"You chose to see your stored information."<<endl;
display_list();
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;
}
[/code]
here's my updated code
[code = cplusplus]
// expenses linked list.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using std::memset;
using namespace std;
//linked list node creation
struct node
{
char name[20]; //dynamic allocation for name
char cat[10]; //dynamic allocation for category
float cost; //$ amount
node *next; //links to next node
};
node* head=NULL;
node* current; //used to traverse through list
int option = 0;
void add_expense_at_end()
{
node *temp, *temp2; //temp pointers
//char tmp[201]; char tmp2[201];
// reserve space for new node and fill it with data
temp = new node;
cout<<"Creating an expense..."<<endl;
cout<<endl;
cout<<"Enter the name of the expense(characters no space!): ";
cin>>temp->name;
cout<<"Enter the category description(characters no space!): (MONTHLY, MISC, RENT, PAYMNT, FUN) ";
cin>>temp->cat;
cout<<"What's the cost of this expense?(numbers only) $";
cin>>temp->cost;
temp->next= NULL; //end of node
//link to node
if(head==NULL)
{
head=temp;
current=head;
}
else //list is not empty
{
temp2= head;
while(temp2 -> next != NULL)
{
temp2 = temp2 -> next; //move to next link
}
temp2 -> next = temp;
}
}
void display_list()
{
node *temp;
temp = head;
cout<<endl;
if (temp==NULL)
cout<<"Empty list"<<endl;
else
{
while(temp!=NULL) //display details for what temp points too
{
cout<<"Name: "<<temp->name<<" ";
cout<<"Category: "<<temp->cat<<" ";
cout<<"Cost: $"<<temp->cost<<" ";
if(temp==current)
cout<<" <--Current node";
cout<<endl;
temp = temp->next;
}
cout<<"End of expenses!"<<endl;
}
}
void delete_start_expense() //delete function for removing an expense from list
{
node *temp;
temp = head;
head = head->next;
delete temp;
}
void delete_end_expense()
{
node *temp1, *temp2;
if (head == NULL)
cout<<"The list is empty!"<<endl;
else
{
temp1=head;
if(temp1->next==NULL)
{
delete temp1;
head = NULL;
}
else
{
while(temp1->next !=NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
}
}
void move_current_on()
{
if(current->next==NULL)
cout<<"This is the last expense item."<<endl;
else
current = current->next;
}
void move_current_back()
{
if(current==head)
cout<<"This is the first expense!"<<endl;
else{
node *tail; //pointer declaration
tail = head;
while(tail->next != current)
{
tail = tail->next;
}
current = tail;
}
}
void linked_list() //linked list manager function
{
head=NULL;
do
{
cout<<endl;
cout<<"Welcome to the linked-list manager function!"<<endl;
display_list();
cout<<endl;
cout<<"Please select an option: "<<endl;
cout<<endl;
cout<<"0. Exit the manager (*Save to external file before exiting program to store list)"<<endl;
cout<<endl;
cout<<"1. Add an expense to the end of the list."<<endl;
cout<<endl;
cout<<"2. Delete first expense from the list."<<endl;
cout<<endl;
cout<<"3. Delete last expense from the list."<<endl;
cout<<endl;
cout<<"4. Traverse to next expense by one node."<<endl;
cout<<endl;
cout<<"5. Traverse the current expense back by one node."<<endl;
cout<<endl;
cout<<" Enter choice number, followed by enter: ";
cin>>option;
cout<<endl;
switch (option)
{
case 1: add_expense_at_end();break;
case 2: delete_start_expense();break;
case 3: delete_end_expense();break;
case 4: move_current_on();break;
case 5: move_current_back();break;
}
}
while (option !=0);
}
void write() //function for writing to external data file
{
ofstream outfile;
outfile.open("mylist.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; //link to next expense
}
outfile.close();
}
void read() //read in function
{
static const size_t buf_size = 200; //string copy size limit
ifstream infile;
infile.open("mylist.txt");
// infile.open();
if(infile.fail())
{
cout<<"Failed to open file..."<<endl;
return;
}
char aray[buf_size];
node *current= NULL;
memset (aray, 0, buf_size);
infile.getline(aray, buf_size,'~');
while(!infile.eof())
{
current = head;
head = new node;
head->next = current;
head->name; //= new char [strlen(aray)+1];
//strncpy(head->name, aray, buf_size-1);
infile.getline(aray, buf_size, '~');infile.get(); //eat delimeter
head->cat; //= new char[strlen(aray)+1];
//strncpy(head->cat, aray, buf_size-1);
infile.getline(aray,buf_size,'~');infile.get(); //eat delimeter
infile>>head->cost; infile.get(); //eat the newline
cout<<aray<<endl;
}
// infile.close;
}
class income
{
private:
float balance;
float cash;
public:
income();
void deposit();
void display_income();
void withdrawal();
void write();
};
income::income()
{
balance = 0.0;
cash = 0.0;
}
void deposit ()
{
cout<<"You chose to add some money into your account!"<<endl;
cout<<endl;
cout<"Enter the cash amount you have to add: $";
cin>>cash;
}
void income::write()
{
ofstream out;
out.open("money.txt");
if(!out.is_open())
cout<<"Couldn't open the file..."<<endl;
out<<balance<<'\n';
out.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
// 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. Choose to go to expense list manager"<<endl;
cout<<endl;
cout<<"3. Choose to save to external file before quitting program"<<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. Open expense list manager."<<endl;
linked_list();
break;
case 3:
cout<<"You chose option 3. Save expense to external file."<<endl;
write();
break;
case 4:
cout<<"You chose option 4.Write created expenses to file"<<endl;
case 5:
break;
case 6:
cout<<"You chose to see your stored information."<<endl;
display_list();
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;
}
[/code]
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
// expenses linked list.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <fstream> using std::memset; using namespace std; //linked list node creation struct node { char name[20]; //dynamic allocation for name char cat[10]; //dynamic allocation for category float cost; //$ amount node *next; //links to next node }; node* head=NULL; node* current; //used to traverse through list int option = 0; void add_expense_at_end() { node *temp, *temp2; //temp pointers //char tmp[201]; char tmp2[201]; // reserve space for new node and fill it with data temp = new node; cout<<"Creating an expense..."<<endl; cout<<endl; cout<<"Enter the name of the expense(characters no space!): "; cin>>temp->name; cout<<"Enter the category description(characters no space!): (MONTHLY, MISC, RENT, PAYMNT, FUN) "; cin>>temp->cat; cout<<"What's the cost of this expense?(numbers only) $"; cin>>temp->cost; temp->next= NULL; //end of node //link to node if(head==NULL) { head=temp; current=head; } else //list is not empty { temp2= head; while(temp2 -> next != NULL) { temp2 = temp2 -> next; //move to next link } temp2 -> next = temp; } } void display_list() { node *temp; temp = head; cout<<endl; if (temp==NULL) cout<<"Empty list"<<endl; else { while(temp!=NULL) //display details for what temp points too { cout<<"Name: "<<temp->name<<" "; cout<<"Category: "<<temp->cat<<" "; cout<<"Cost: $"<<temp->cost<<" "; if(temp==current) cout<<" <--Current node"; cout<<endl; temp = temp->next; } cout<<"End of expenses!"<<endl; } } void delete_start_expense() //delete function for removing an expense from list { node *temp; temp = head; head = head->next; delete temp; } void delete_end_expense() { node *temp1, *temp2; if (head == NULL) cout<<"The list is empty!"<<endl; else { temp1=head; if(temp1->next==NULL) { delete temp1; head = NULL; } else { while(temp1->next !=NULL) { temp2 = temp1; temp1 = temp1->next; } delete temp1; temp2->next = NULL; } } } void move_current_on() { if(current->next==NULL) cout<<"This is the last expense item."<<endl; else current = current->next; } void move_current_back() { if(current==head) cout<<"This is the first expense!"<<endl; else{ node *tail; //pointer declaration tail = head; while(tail->next != current) { tail = tail->next; } current = tail; } } void linked_list() //linked list manager function { head=NULL; do { cout<<endl; cout<<"Welcome to the linked-list manager function!"<<endl; display_list(); cout<<endl; cout<<"Please select an option: "<<endl; cout<<endl; cout<<"0. Exit the manager (*Save to external file before exiting program to store list)"<<endl; cout<<endl; cout<<"1. Add an expense to the end of the list."<<endl; cout<<endl; cout<<"2. Delete first expense from the list."<<endl; cout<<endl; cout<<"3. Delete last expense from the list."<<endl; cout<<endl; cout<<"4. Traverse to next expense by one node."<<endl; cout<<endl; cout<<"5. Traverse the current expense back by one node."<<endl; cout<<endl; cout<<" Enter choice number, followed by enter: "; cin>>option; cout<<endl; switch (option) { case 1: add_expense_at_end();break; case 2: delete_start_expense();break; case 3: delete_end_expense();break; case 4: move_current_on();break; case 5: move_current_back();break; } } while (option !=0); } void write() //function for writing to external data file { ofstream outfile; outfile.open("mylist.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; //link to next expense } outfile.close(); } void read() //read in function { static const size_t buf_size = 200; //string copy size limit ifstream infile; infile.open("mylist.txt"); // infile.open(); if(infile.fail()) { cout<<"Failed to open file..."<<endl; return; } char aray[buf_size]; node *current= NULL; memset (aray, 0, buf_size); infile.getline(aray, buf_size,'~'); while(!infile.eof()) { current = head; head = new node; head->next = current; head->name; //= new char [strlen(aray)+1]; //strncpy(head->name, aray, buf_size-1); infile.getline(aray, buf_size, '~');infile.get(); //eat delimeter head->cat; //= new char[strlen(aray)+1]; //strncpy(head->cat, aray, buf_size-1); infile.getline(aray,buf_size,'~');infile.get(); //eat delimeter infile>>head->cost; infile.get(); //eat the newline cout<<aray<<endl; } // infile.close; } class income { private: float balance; float cash; public: income(); void deposit(); void display_income(); void withdrawal(); void write(); }; income::income() { balance = 0.0; cash = 0.0; } void deposit () { cout<<"You chose to add some money into your account!"<<endl; cout<<endl; cout<"Enter the cash amount you have to add: $"; cin>>cash; } void income::write() { ofstream out; out.open("money.txt"); if(!out.is_open()) cout<<"Couldn't open the file..."<<endl; out<<balance<<'\n'; out.close(); } int _tmain(int argc, _TCHAR* argv[]) { // 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. Choose to go to expense list manager"<<endl; cout<<endl; cout<<"3. Choose to save to external file before quitting program"<<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. Open expense list manager."<<endl; linked_list(); break; case 3: cout<<"You chose option 3. Save expense to external file."<<endl; write(); break; case 4: cout<<"You chose option 4.Write created expenses to file"<<endl; case 5: break; case 6: cout<<"You chose to see your stored information."<<endl; display_list(); 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; }
![]() |
Similar Threads
- Using Linked List to write to a file (C)
- Reading a list from txt file to array... (Pascal and Delphi)
- reading a specific data in .txt file to put in specific tag in xml in vb.net (VB.NET)
- linked list and text file (C++)
- Linked List program HELP (C++)
- Reading multiple lines from a txt file (C++)
- Adding to linked list from external file (C)
- How to read in a sentence and insert in to linked list? (C++)
Other Threads in the C++ Forum
- Previous Thread: Recursive algorithm
- Next Thread: compiler error message c4430
Views: 515 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






