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: tizzo233 is an unknown quantity at this point 
Solved Threads: 0
tizzo233 tizzo233 is offline Offline
Newbie Poster

Help me please with reading in linked list txt file

 
0
  #1
May 18th, 2009
  1. // s2.cpp : Defines the entry point for the console application.
  2. // Project Stage 2.cpp : Defines the entry point for the console application.
  3. /* Algorithm
  4. Menu()
  5. Choose option
  6. Create a new expense
  7. Enter the initial amount of money in bank or to add money to bank from paycheck
  8. Enter the new expense
  9. Enter the expense category
  10. Input whether expense is recurring or not*
  11. Input whether expense is saved for or not
  12. calculate balance after expense
  13. display financial status*/
  14.  
  15.  
  16.  
  17. #include <iostream>
  18. #include <cstring>
  19. #include <cassert>
  20. #include <fstream>
  21.  
  22. using std::strncpy;
  23. using std::memset;
  24.  
  25.  
  26. using namespace std;
  27.  
  28.  
  29. struct node
  30. {
  31. char *name;//dynamic array for name of expense
  32. char *cat;//category of expense
  33. float cost;//cost of expense
  34. void create();
  35. node* next;
  36. node();//node constructor
  37.  
  38. };
  39.  
  40.  
  41.  
  42. class expense
  43. {
  44. private:
  45. node *head;
  46. float balance;
  47. float cash;
  48. public:
  49. expense();//constructor
  50. ~expense();//destructor
  51. void create();//function to create expense
  52. void display();//display function for expenses
  53. void read();
  54. void write();
  55. void deposit();
  56. void add();//add expenses total
  57. void status();
  58. };
  59. expense::expense()
  60. {
  61. head = NULL;
  62. }
  63. expense::~expense()
  64. {
  65. node* current=NULL;
  66. while(head)
  67. {
  68. current = head->next;
  69. if(head->name)
  70. delete [] head-> name;
  71. if(head->cat)
  72. delete [] head->cat;
  73. head->cost = 0.0;
  74. delete head;
  75. head = current;
  76. }
  77. }
  78. void expense::deposit()
  79. {
  80. cout<<"You have chosen the option to deposit money into your revolving account."<<endl;
  81. cout<<" "<<endl;
  82. cout<<"Enter the amount you would like to deposit: $ ";
  83. cin>>cash;
  84. cout<<" "<<endl;
  85. cout<<"To see how much you have, and if you can afford purchase choose option financial status"<<endl;
  86. }
  87. void expense::add()//function that adds all expenses
  88. {
  89. //code for adding my cost
  90. //return total
  91. //total variable for expense totals
  92. }
  93.  
  94. void expense::status() //function that tells you whether or not you can afford a purchase
  95. {
  96. float price=0;
  97. cout<<"What is the price of your intended purchase: $ "<<endl;
  98. cin>>price;
  99. //if price greater than balance then can't afford new purchase
  100. //if price less than balance, then you can afford new purchase
  101. }
  102.  
  103.  
  104. void expense::display() //this function displays the expenses you have just created as well as those read in from external file
  105. {
  106. node*current = head;
  107. while(current)
  108. {
  109. cout<<" "<<endl;
  110. cout<<current->name<<" "<<current->cat<<" $"<<current->cost<<'\n'; //display format same as the format of data file
  111. cout<<" "<<endl;
  112. current = current->next;
  113. }
  114. }
  115.  
  116. void expense::read()
  117. {
  118. static const size_t buf_size = 200;
  119. ifstream infile;
  120. infile.open("expense.txt");
  121.  
  122. if (infile.fail()) {
  123. cout<<"Failed to open the file..." << endl;
  124. return;
  125. }
  126.  
  127. char aray[buf_size];
  128. node*current = NULL;
  129.  
  130. memset( aray, 0, buf_size ); // Set array to zeroes.
  131. infile.getline(aray,buf_size,':');
  132. int temp=0;
  133.  
  134. while(!infile.eof())
  135. {
  136. current = head; //Initialize values
  137. //head = NULL;
  138. //head = new node;
  139. //head->name = NULL;
  140. //head->next = NULL;
  141. //head->cat = NULL;
  142. //head->cost=0.0;
  143. infile.get();
  144. // head->next=current;
  145. head->name = new char[strlen(aray)+1];
  146. strncpy(head->name, aray, buf_size-1); // string length
  147. infile.getline(aray,buf_size,':');
  148. infile.get(); // eat colon
  149. head->cat = new char[strlen(aray)+1];
  150. strncpy(head->cat, aray, (buf_size-1));
  151. infile.get(); infile.get(aray, (buf_size), ':'); //eat the colon
  152. //infile.get();
  153. infile >> head->cost;
  154. infile.get();//eat the '/n'
  155. infile.getline(aray,buf_size,'\n');//get cost
  156. cout<<aray<<endl;
  157. head->cost=temp;
  158. infile.get();
  159. infile.getline(aray,buf_size,':');
  160. assert(head);
  161. //cout << head->name << ':';
  162. //cout << head->cat << ':';
  163. //cout << head->cost << '\n';
  164. cout<<"hi"<<endl;
  165. }
  166.  
  167. infile.close();
  168.  
  169. }
  170.  
  171. void expense::write()
  172. {
  173. ofstream outfile;
  174. outfile.open("expense.txt");
  175. node *current = head;
  176. if(!outfile.is_open())
  177. cout<<"couldn't open the file"<<endl;
  178. while(current!=NULL)
  179. {
  180. outfile<<current->name<<":"<<current->cat<<":"<<current->cost<<'\n';
  181. cout<<current->name<<":"<<current->cat<<":"<<current->cost<<'\n';
  182. current=current->next;
  183. }
  184. outfile.close();
  185. }
  186. /*class expense_track::expense //access to class expense
  187. {
  188.   public:
  189.   expenseTrack();
  190.   void displayallexpenes();
  191.   void expensetotalshow();
  192.   int *search (char cat[]);
  193.   void deletexpense();
  194.   private:
  195.   int numexpenses;
  196. };
  197.  
  198. expenseTrack::expenseTrack()
  199. {
  200.   numexpenses=0;//data member
  201. }*/
  202.  
  203. //member functions to describe expense class
  204. void node::create()
  205. {
  206. char temp[201];
  207. char temp2[201];
  208. cout<<"Creating a new expense....."<<endl;
  209. cout<<"Enter the name of the expense: ";
  210. cin>>temp;
  211. name=new char[strlen(temp)+1];
  212. strcpy(name,temp);
  213. cout<<"Enter the expense type: (MONTHLY/MISC/TAX/LUXURY) ";
  214. cin>>temp2;
  215. cat = new char[strlen(temp2)+1];
  216. strcpy(cat,temp2);
  217. cout<<"Enter the cost of the expense: $";
  218. cin>>cost;
  219. }
  220. void expense::create()
  221. {
  222. node*current = head;
  223. head= new node;
  224. head->create();
  225. head-> next = current;
  226. }
  227.  
  228.  
  229. int main ()
  230. {
  231. expense exp;
  232. int choice=0;
  233. int i;i=0;
  234. cout<<"Welcome to TONDRE'S BUDGET ANALYZER Program"<<endl;
  235. cout<<"*******************************************"<<endl;
  236. cout<<" "<<endl;
  237. while(choice!=5)
  238. {
  239. cout<<"Please select from one of the following options"<<endl;
  240. cout<<" "<<endl;
  241. cout<<"1. Deposit money into your account"<<endl;
  242. cout<<" "<<endl;
  243. cout<<"2. Create an expense"<<endl;
  244. cout<<" "<<endl;
  245. cout<<"3. Display the expenses you have entered"<<endl;
  246. cout<<" "<<endl;
  247. cout<<"4. Save these expenses to an external file"<<endl;
  248. cout<<" "<<endl;
  249. cout<<"5. Choose to quit."<<endl;
  250. cout<<" "<<endl;
  251. cout<<"6. Choose to see stored expenses on external file."<<endl;
  252. cout<<" "<<endl;
  253. cout<<"Type in a choice."<<endl;
  254. cout<<" "<<endl;
  255.  
  256. cin>>choice;
  257. cout<<" "<<endl;
  258. switch (choice)
  259. {
  260. case 1:
  261. cout<<"You chose option 1. You want to deposit some money into your account. "<<endl;
  262. //exp.income();
  263. break;
  264. case 2:
  265. cout<<"You chose option 2. Create a new expense."<<endl;
  266. exp.create();
  267. break;
  268. case 3:
  269. cout<<"You chose option 3. Review and display expenses list."<<endl;
  270. // exp.read();
  271. exp.display();
  272. break;
  273. case 4:
  274. cout<<"You chose option 4.Write created expenses to file"<<endl;
  275. exp.write();
  276.  
  277. case 5:
  278. break;
  279. case 6:
  280. cout<<"You chose to see your stored information."<<endl;
  281. exp.read();
  282. exp.display();
  283. break;
  284. default:
  285. cout<<"You have made an illegal choice."<<endl;
  286. }
  287. }
  288. char reply;
  289. cout << "Press q (or any other key) followed by 'Enter' to quit: ";
  290. cin >> reply;
  291. return 0;
  292. }

I can't do option 6. Reading in from external file. I get seg errors
Attached Files
File Type: txt expense.txt (28 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help me please with reading in linked list txt file

 
0
  #2
May 18th, 2009
Of course, you got memory access exception: initial head == 0 but you are trying to assign new node pointer via this null pointer:
  1. head->name = new char ...
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 ).
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: tizzo233 is an unknown quantity at this point 
Solved Threads: 0
tizzo233 tizzo233 is offline Offline
Newbie Poster

Re: Help me please with reading in linked list txt file

 
0
  #3
May 18th, 2009
sorry i don't understand. you mean i should set head= new node;//i already did. doesn't that set it?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help me please with reading in linked list txt file

 
0
  #4
May 18th, 2009
>i already did.
Where? If the 1st choice is 6, head == NULL after constructor.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Help me please with reading in linked list txt file

 
0
  #5
May 18th, 2009
Originally Posted by tizzo233 View Post
sorry i don't understand. you mean i should set head= new node;//i already did. doesn't that set it?
I'm sorry, but I have to agree with ArkM - there's a ton of mistakes in the provided code. Just go through it, step by step and straighten out the code.

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."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: tizzo233 is an unknown quantity at this point 
Solved Threads: 0
tizzo233 tizzo233 is offline Offline
Newbie Poster

Re: Help me please with reading in linked list txt file

 
0
  #6
May 19th, 2009
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
  1. struct node
  2. {
  3. char *name;//dynamic array for name of expense
  4. char *cat;//category of expense
  5. float cost;//cost of expense
  6. void create();
  7. node* next;
  8.  
  9. };
  10.  
  11.  
  12. class expense
  13. {
  14. private:
  15. node *head;
  16. public:
  17. expense();//constructor
  18. ~expense();//destructor
  19. void create();//function to create expense
  20. void display();//display function for expenses
  21. void read();
  22. void write();
  23. //void add();//add expenses total
  24. };
  25. expense::expense()
  26. {
  27. head = NULL;
  28. head->cost = 0.0;
  29. }
  30. expense::~expense()
  31. {
  32. node* current=NULL;
  33. while(head)
  34. {
  35. current = head->next;
  36. if(head->name)
  37. delete [] head-> name;
  38. if(head->cat)
  39. delete [] head->cat;
  40.  
  41. delete head;
  42. head = current;
  43. }
  44. }
  45. //void expense::add()
  46. //function to add all my expenses. ie. sum of cost from file
  47. void expense::display()
  48. {
  49. node*current = head;
  50. while(current)
  51. {
  52. cout<<" "<<endl;
  53. cout<<current->name<<" "<<current->cat<<" $"<<current->cost<<endl;
  54. current = current->next;
  55. }
  56. }
  57.  
  58. void expense::read()
  59. {
  60. static const size_t buf_size = 200;
  61. ifstream infile;
  62. infile.open("expense.txt");
  63.  
  64. if (infile.fail()) {
  65. cout<<"Failed to open the file..." << endl;
  66. return;
  67. }
  68.  
  69. char aray[buf_size];
  70. node*current = NULL;
  71.  
  72. memset( aray, 0, buf_size ); // Set array to zeroes.
  73. infile.getline(aray,buf_size,':');
  74. int temp=0;
  75.  
  76. while(!infile.eof())
  77. {
  78. current = head; //Initialize values
  79. head = NULL;
  80. head = new node;
  81. head->name = NULL;
  82. head->next = NULL;
  83. head->cat = NULL;
  84. head->cost=0.0;
  85.  
  86. head->next=current;
  87. head->name = new char[strlen(aray)+1];
  88. strncpy(head->name, aray, buf_size-1); // string length
  89. infile.getline(aray,buf_size,':');
  90. infile.get(); // eat colon
  91. head->cat = new char[strlen(aray)+1];
  92. strncpy(head->cat, aray, buf_size-1);
  93. infile.getline(aray,buf_size,':');
  94. infile.get();
  95. infile >> head->cost;
  96. infile.get();//eat the '/n'
  97. //outfile.getline(aray,buf_size,'\n');//get cost sort of
  98. //cout<<aray<<endl;
  99. //head->cost=temp;
  100. //infile.get();
  101. //infile.getline(aray,buf_size,':');
  102. assert(head);
  103. cout << head->name << ":";
  104. cout << head->cat << ":";
  105. cout << head->cost << '\n';
  106. }
  107.  
  108. infile.close();
  109.  
  110. }
  111.  
  112. void expense::write()
  113. {
  114. ofstream outfile;
  115. outfile.open("expense.txt");
  116. node *current = head;
  117. if(!outfile.is_open())
  118. cout<<"couldn't open the file"<<endl;
  119. while(current!=NULL)
  120. {
  121. outfile<<current->name<<":"<<current->cat<<":"<<current->cost<<'\n';
  122. cout<<current->name<<":"<<current->cat<<":"<<current->cost<<'\n';
  123. current=current->next;
  124. }
  125. outfile.close();
  126. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help me please with reading in linked list txt file

 
0
  #7
May 19th, 2009
>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:
  1. allocate a new node, fill its data fields, set next member to NULL.
  2. if a list is empty (head == NULL)
  3. set head to the new node; all done
  4. else
  5. find a pointer to the last list node *)
  6. set this node.next to the new node
  7. endif
*) 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):
  1. expense::expense()
  2. {
  3. head = NULL;
  4. head->cost = 0.0;
  5. }
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...
Last edited by ArkM; May 19th, 2009 at 6:05 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Help me please with reading in linked list txt file

 
0
  #8
May 19th, 2009
Originally Posted by tizzo233 View Post
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.
A tutorial is supposed to teach you methods of doing something: in this case - "How to build a singly linked list". You could have easily emulated the workings of that list and incorporated it in your own code. Try doing that first, and do not worry about the data types involved in it. It is better to grasp the concepts first before going the whole hog and trying a more complicated program.

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."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: tizzo233 is an unknown quantity at this point 
Solved Threads: 0
tizzo233 tizzo233 is offline Offline
Newbie Poster

Re: Help me please with reading in linked list txt file

 
0
  #9
May 19th, 2009
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]
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: tizzo233 is an unknown quantity at this point 
Solved Threads: 0
tizzo233 tizzo233 is offline Offline
Newbie Poster

Re: Help me please with reading in linked list txt file

 
0
  #10
May 19th, 2009
  1. // expenses linked list.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7.  
  8. using std::memset;
  9. using namespace std;
  10.  
  11. //linked list node creation
  12. struct node
  13. {
  14. char name[20]; //dynamic allocation for name
  15. char cat[10]; //dynamic allocation for category
  16. float cost; //$ amount
  17. node *next; //links to next node
  18. };
  19.  
  20. node* head=NULL;
  21. node* current; //used to traverse through list
  22. int option = 0;
  23.  
  24.  
  25.  
  26. void add_expense_at_end()
  27. {
  28. node *temp, *temp2; //temp pointers
  29. //char tmp[201]; char tmp2[201];
  30. // reserve space for new node and fill it with data
  31. temp = new node;
  32. cout<<"Creating an expense..."<<endl;
  33. cout<<endl;
  34. cout<<"Enter the name of the expense(characters no space!): ";
  35. cin>>temp->name;
  36. cout<<"Enter the category description(characters no space!): (MONTHLY, MISC, RENT, PAYMNT, FUN) ";
  37. cin>>temp->cat;
  38. cout<<"What's the cost of this expense?(numbers only) $";
  39. cin>>temp->cost;
  40. temp->next= NULL; //end of node
  41.  
  42. //link to node
  43. if(head==NULL)
  44. {
  45. head=temp;
  46. current=head;
  47. }
  48. else //list is not empty
  49. {
  50. temp2= head;
  51. while(temp2 -> next != NULL)
  52. {
  53. temp2 = temp2 -> next; //move to next link
  54. }
  55. temp2 -> next = temp;
  56. }
  57. }
  58.  
  59. void display_list()
  60. {
  61. node *temp;
  62. temp = head;
  63. cout<<endl;
  64. if (temp==NULL)
  65. cout<<"Empty list"<<endl;
  66.  
  67. else
  68. {
  69. while(temp!=NULL) //display details for what temp points too
  70. {
  71. cout<<"Name: "<<temp->name<<" ";
  72. cout<<"Category: "<<temp->cat<<" ";
  73. cout<<"Cost: $"<<temp->cost<<" ";
  74. if(temp==current)
  75. cout<<" <--Current node";
  76. cout<<endl;
  77. temp = temp->next;
  78. }
  79. cout<<"End of expenses!"<<endl;
  80. }
  81. }
  82.  
  83. void delete_start_expense() //delete function for removing an expense from list
  84. {
  85. node *temp;
  86. temp = head;
  87. head = head->next;
  88. delete temp;
  89. }
  90.  
  91. void delete_end_expense()
  92. {
  93. node *temp1, *temp2;
  94. if (head == NULL)
  95. cout<<"The list is empty!"<<endl;
  96. else
  97. {
  98. temp1=head;
  99. if(temp1->next==NULL)
  100. {
  101. delete temp1;
  102. head = NULL;
  103. }
  104. else
  105. {
  106. while(temp1->next !=NULL)
  107. {
  108. temp2 = temp1;
  109. temp1 = temp1->next;
  110. }
  111. delete temp1;
  112. temp2->next = NULL;
  113. }
  114. }
  115. }
  116.  
  117. void move_current_on()
  118. {
  119. if(current->next==NULL)
  120. cout<<"This is the last expense item."<<endl;
  121. else
  122. current = current->next;
  123. }
  124.  
  125. void move_current_back()
  126. {
  127. if(current==head)
  128. cout<<"This is the first expense!"<<endl;
  129. else{
  130. node *tail; //pointer declaration
  131. tail = head;
  132.  
  133. while(tail->next != current)
  134. {
  135. tail = tail->next;
  136. }
  137. current = tail;
  138. }
  139. }
  140.  
  141. void linked_list() //linked list manager function
  142. {
  143. head=NULL;
  144. do
  145. {
  146. cout<<endl;
  147. cout<<"Welcome to the linked-list manager function!"<<endl;
  148. display_list();
  149. cout<<endl;
  150. cout<<"Please select an option: "<<endl;
  151. cout<<endl;
  152. cout<<"0. Exit the manager (*Save to external file before exiting program to store list)"<<endl;
  153. cout<<endl;
  154. cout<<"1. Add an expense to the end of the list."<<endl;
  155. cout<<endl;
  156. cout<<"2. Delete first expense from the list."<<endl;
  157. cout<<endl;
  158. cout<<"3. Delete last expense from the list."<<endl;
  159. cout<<endl;
  160. cout<<"4. Traverse to next expense by one node."<<endl;
  161. cout<<endl;
  162. cout<<"5. Traverse the current expense back by one node."<<endl;
  163. cout<<endl;
  164. cout<<" Enter choice number, followed by enter: ";
  165. cin>>option;
  166. cout<<endl;
  167. switch (option)
  168. {
  169. case 1: add_expense_at_end();break;
  170. case 2: delete_start_expense();break;
  171. case 3: delete_end_expense();break;
  172. case 4: move_current_on();break;
  173. case 5: move_current_back();break;
  174. }
  175. }
  176.  
  177. while (option !=0);
  178.  
  179. }
  180. void write() //function for writing to external data file
  181. {
  182. ofstream outfile;
  183. outfile.open("mylist.txt");
  184. node *current = head;
  185. if(!outfile.is_open())
  186. cout<<"Couldn't open the file"<<endl;
  187. while(current!=NULL)
  188. {
  189. outfile<<current->name<<"~"<<current->cat<<"~"<<current->cost<<'\n';
  190. cout<<current->name<<"~"<<current->cat<<"~"<<current->cost<<'\n';
  191. current = current->next; //link to next expense
  192. }
  193. outfile.close();
  194. }
  195.  
  196. void read() //read in function
  197. {
  198. static const size_t buf_size = 200; //string copy size limit
  199. ifstream infile;
  200. infile.open("mylist.txt");
  201. // infile.open();
  202. if(infile.fail())
  203. {
  204. cout<<"Failed to open file..."<<endl;
  205. return;
  206. }
  207.  
  208. char aray[buf_size];
  209. node *current= NULL;
  210.  
  211. memset (aray, 0, buf_size);
  212. infile.getline(aray, buf_size,'~');
  213.  
  214. while(!infile.eof())
  215. {
  216. current = head;
  217. head = new node;
  218. head->next = current;
  219. head->name; //= new char [strlen(aray)+1];
  220. //strncpy(head->name, aray, buf_size-1);
  221. infile.getline(aray, buf_size, '~');infile.get(); //eat delimeter
  222. head->cat; //= new char[strlen(aray)+1];
  223. //strncpy(head->cat, aray, buf_size-1);
  224. infile.getline(aray,buf_size,'~');infile.get(); //eat delimeter
  225. infile>>head->cost; infile.get(); //eat the newline
  226. cout<<aray<<endl;
  227. }
  228. // infile.close;
  229. }
  230.  
  231. class income
  232. {
  233. private:
  234. float balance;
  235. float cash;
  236. public:
  237. income();
  238. void deposit();
  239. void display_income();
  240. void withdrawal();
  241. void write();
  242. };
  243. income::income()
  244. {
  245. balance = 0.0;
  246. cash = 0.0;
  247. }
  248.  
  249. void deposit ()
  250. {
  251. cout<<"You chose to add some money into your account!"<<endl;
  252. cout<<endl;
  253. cout<"Enter the cash amount you have to add: $";
  254. cin>>cash;
  255. }
  256. void income::write()
  257. {
  258. ofstream out;
  259. out.open("money.txt");
  260. if(!out.is_open())
  261. cout<<"Couldn't open the file..."<<endl;
  262. out<<balance<<'\n';
  263. out.close();
  264. }
  265.  
  266. int _tmain(int argc, _TCHAR* argv[])
  267. {
  268. // expense exp;
  269. int choice=0;
  270. int i;i=0;
  271. cout<<"Welcome to TONDRE'S BUDGET ANALYZER Program"<<endl;
  272. cout<<"*******************************************"<<endl;
  273. cout<<" "<<endl;
  274. while(choice!=5)
  275. {
  276. cout<<"Please select from one of the following options"<<endl;
  277. cout<<endl;
  278. cout<<"1. Deposit money into your account"<<endl;
  279. cout<<endl;
  280. cout<<"2. Choose to go to expense list manager"<<endl;
  281. cout<<endl;
  282. cout<<"3. Choose to save to external file before quitting program"<<endl;
  283. cout<<endl;
  284. cout<<"4. Save these expenses to an external file"<<endl;
  285. cout<<endl;
  286. cout<<"5. Choose to quit."<<endl;
  287. cout<<endl;
  288. cout<<"6. Choose to see stored expenses on external file."<<endl;
  289. cout<<endl;
  290. cout<<"Type in a choice."<<endl;
  291. cout<<endl;
  292.  
  293. cin>>choice;
  294. cout<<endl;
  295. switch (choice)
  296. {
  297. case 1:
  298. cout<<"You chose option 1. You want to deposit some money into your account. "<<endl;
  299. //exp.income();
  300. break;
  301. case 2:
  302. cout<<"You chose option 2. Open expense list manager."<<endl;
  303. linked_list();
  304. break;
  305. case 3:
  306. cout<<"You chose option 3. Save expense to external file."<<endl;
  307. write();
  308. break;
  309. case 4:
  310. cout<<"You chose option 4.Write created expenses to file"<<endl;
  311.  
  312. case 5:
  313. break;
  314. case 6:
  315. cout<<"You chose to see your stored information."<<endl;
  316. display_list();
  317. break;
  318. default:
  319. cout<<"You have made an illegal choice."<<endl;
  320. }
  321. }
  322. char reply;
  323. cout << "Press q (or any other key) followed by 'Enter' to quit: ";
  324. cin >> reply;
  325. return 0;
  326. }
Attached Files
File Type: txt mylist.txt (49 Bytes, 2 views)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 515 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC