Deleting Structured Array Entry

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 7
Reputation: bolx is an unknown quantity at this point 
Solved Threads: 0
bolx bolx is offline Offline
Newbie Poster

Deleting Structured Array Entry

 
0
  #1
Apr 20th, 2009
Hi there, i have an assignment, and i need to create a monthly expenditure. I have managed to get alot done, a fully functioning menu and all the functions. Im stuck when it comes to deleting an entry

I have been using structured arrays to store data, ive posted all my code, i thought it be easier for help, sorry for the massive post.

  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. int i;
  10. int count;
  11. int userChoice;
  12. string temp;
  13.  
  14. int newExpenses();
  15. int newIncome();
  16. void menuExpenses();
  17. void menuIncome();
  18. void mainMenu();
  19. void monthlyExpenses();
  20. void monthlyIncome();
  21. void deleteEntry();
  22.  
  23.  
  24. int main()
  25. {
  26.  
  27. string line;
  28. ifstream myfile ("counter.txt");
  29. if(myfile.is_open())
  30. {
  31. while(!myfile.eof())
  32. {
  33. getline(myfile,line);
  34. temp=line;
  35. }
  36. myfile.close();
  37. }
  38.  
  39.  
  40. count=atoi(temp.c_str());
  41.  
  42. do{
  43. mainMenu();
  44. }while(userChoice != 1 || userChoice != 2);
  45.  
  46. return 0;
  47. }
  48.  
  49. struct entry{
  50. int ID;
  51. string date;
  52. string item;
  53. string price;
  54. string category;
  55. }entries[100];
  56.  
  57. void mainMenu()
  58. {
  59. cout<<"Monthly Expenditure"<<endl<<endl<<endl;
  60. cout<<"Main Menu"<<endl<<endl;
  61. cout<<"Income [1]"<<endl<<"Expenses [2]"<<endl<<"View Monthly Income [3]"<<endl<<"View Monthly Expenses [4]"<<endl<<"Delete Entry [5]"<<endl;
  62. cin>>userChoice;
  63.  
  64. if(userChoice == 1)
  65. {
  66. system("cls");
  67. menuIncome();
  68. }
  69.  
  70. else if(userChoice == 2)
  71. {
  72. system("cls");
  73. menuExpenses();
  74. }
  75.  
  76. else if(userChoice == 3)
  77. {
  78. system("cls");
  79. monthlyIncome();
  80. }
  81.  
  82. else if(userChoice == 4)
  83. {
  84. system("cls");
  85. monthlyExpenses();
  86. }
  87.  
  88. else if(userChoice == 5)
  89. {
  90. system("cls");
  91. deleteEntry();
  92. }
  93.  
  94. else
  95. {
  96. system("cls");
  97. cout<<"Invalid input, please select again [1] [2] [3] [4]";
  98. }
  99. }
  100.  
  101. void monthlyIncome()
  102. {
  103. string line;
  104. ifstream myfile ("Income.txt");
  105. if(myfile.is_open())
  106. {
  107. while(!myfile.eof())
  108. {
  109. getline(myfile,line);
  110. cout<<line<<endl;
  111. }
  112. myfile.close();
  113. }
  114.  
  115. }
  116.  
  117.  
  118. void monthlyExpenses()
  119. {
  120. string line;
  121. ifstream myfile ("Expenses.txt");
  122. if(myfile.is_open())
  123. {
  124. while(!myfile.eof())
  125. {
  126. getline(myfile,line);
  127. cout<<line<<endl;
  128. }
  129. myfile.close();
  130. }
  131.  
  132. }
  133.  
  134. void menuExpenses()
  135. {
  136. int userChoice2=0;
  137.  
  138. do{
  139. cout<<"Expenses"<<endl<<endl;
  140. cout<<"Add Entry (1)"<<endl;
  141. cout<<"Sort Ascending (2)"<<endl;
  142. cout<<"Sort Descending (3)"<<endl;
  143. cin>>userChoice2;
  144.  
  145. if(userChoice2 == 1)
  146. {
  147. system("cls");
  148. newExpenses();
  149. }
  150. else if(userChoice2 == 2)
  151. {
  152. system("cls");
  153. //Sort code
  154. }
  155. else if(userChoice2 == 3)
  156. {
  157. system("cls");
  158. //Sort code
  159. }
  160. else
  161. {
  162. system("cls");
  163. cout<<"Invalid input, please select again [1] [2] [3]"<<endl<<endl;
  164. userChoice2=0;
  165.  
  166. }
  167. }while(userChoice2==0);
  168.  
  169. }
  170.  
  171. void menuIncome()
  172. {
  173. int userChoice3=0;
  174.  
  175. do{
  176.  
  177. cout<<"Income"<<endl<<endl;
  178. cout<<"Add Entry (1)"<<endl;
  179. cout<<"Sort Ascending (2)"<<endl;
  180. cout<<"Sort Descending (3)"<<endl;
  181. cin>>userChoice3;
  182.  
  183. if(userChoice3 == 1)
  184. {
  185. system("cls");
  186. newIncome();
  187. }
  188. else if(userChoice3 == 2)
  189. {
  190. system("cls");
  191. //Sort code
  192. }
  193. else if(userChoice3 == 3)
  194. {
  195. system("cls");
  196. //Sort code
  197. }
  198. else
  199. {
  200. system("cls");
  201. cout<<"Invalid input, please select again (1) (2) (3)"<<endl<<endl;
  202. userChoice3=0;
  203. }
  204. }while(userChoice3==0);
  205.  
  206. }
  207.  
  208. int newIncome()
  209. {
  210. cout<<"Income"<<endl<<endl;
  211. cout<<"New Entry"<<endl;
  212.  
  213. entries[count].ID=count;
  214. cout<<"Date: ";
  215. cin>>entries[count].date;
  216. cout<<"Item: ";
  217. cin>>entries[count].item;
  218. cout<<"Price: ";
  219. cin>>entries[count].price;
  220. cout<<"Category: ";
  221. cin>>entries[count].category;
  222. cout<<endl;
  223.  
  224. ofstream fout;
  225.  
  226. fout.open("Income.txt", ios::app);
  227.  
  228. fout<<entries[count].ID<<" "<<entries[count].date<<" "<<entries[count].item<<" "<<entries[count].category<<" "<<entries[count].price<<endl;
  229. fout<<flush;
  230. fout.close();
  231.  
  232.  
  233.  
  234. count++;
  235.  
  236.  
  237. ofstream fout1;
  238.  
  239. fout1.open ("counter.txt");
  240.  
  241. fout1<<count;
  242. fout1<<flush;
  243. fout1.close();
  244. return 0;
  245. }
  246.  
  247. int newExpenses()
  248. {
  249.  
  250. cout<<"Expenses"<<endl<<endl;
  251. cout<<"New Entry"<<endl;
  252.  
  253. entries[count].ID=count;
  254. cout<<"Date: ";
  255. cin>>entries[count].date;
  256. cout<<"Item: ";
  257. cin>>entries[count].item;
  258. cout<<"Price: ";
  259. cin>>entries[count].price;
  260. cout<<"Category: ";
  261. cin>>entries[count].category;
  262. cout<<endl;
  263.  
  264. ofstream fout;
  265.  
  266. fout.open("Expenses.txt", ios::app);
  267.  
  268. fout<<entries[count].ID<<" "<<entries[count].date<<" "<<entries[count].item<<" "<<entries[count].category<<" "<<entries[count].price<<endl;
  269. fout<<flush;
  270. fout.close();
  271.  
  272.  
  273.  
  274. count++;
  275.  
  276. ofstream fout1;
  277.  
  278. fout1.open ("counter.txt");
  279.  
  280. fout1<<count;
  281. fout1<<flush;
  282. fout1.close();
  283. return 0;
  284. }
  285.  
  286. void deleteEntry()
  287. {
  288. int choice;
  289. int i;
  290.  
  291. cout<<"What entry do you wish to delete?: "<<endl;
  292. cin>>choice;
  293.  
  294. if(choice >= 0 && choice <= 100)
  295. {
  296. for(i=choice;i<100;i++)
  297. {
  298. entries[i] = entries[i + 1];
  299. }
  300. }
  301. else
  302. {
  303. cout<<"Invalid Entry";
  304. }
  305. }

What my tutor suggested was to use a counter, and then save the counter also in a text file, so when i reopen the console, the counter doesnt reset to 0

Is there a simpler way to be able to delete one of these entries? each entry has an ID

(new to programming, still learning at uni)
Last edited by Ancient Dragon; Apr 20th, 2009 at 9:22 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Deleting Structured Array Entry

 
0
  #2
Apr 20th, 2009
1) Move the structure definition in lines 49-55 up above any functions, about line 13 because it makes better overall program organization. Most programs are organized to have all includes at the top followed by structures, classes, enumerations (in any order or in header files), globals and function prototypes. You have most of that already, just need to move the structure declaration to the top of the program.

line 34: why??

lines 64-88: It might be better to use a switch statement instead of all those if statements.

line 294: The array only has 100 elements, numbered 0 to and including 99. So the statement should be < 100, not <= 100.

The only other comment about that function is you need to clear the last structure by setting all strings to empty strings.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: bolx is an unknown quantity at this point 
Solved Threads: 0
bolx bolx is offline Offline
Newbie Poster

Re: Deleting Structured Array Entry

 
0
  #3
Apr 20th, 2009
Thank you for your reply Ancient Dragon, i really appreciate the help, i have moved the structure declaration to line 13 now, and i have changed line 294 also to <100

Line 34: To be honest mate, im not sure either, my tutor helped me on this one, its something to do with changing the integer into a string, so i can save the counter in a text file. But the whole reason for that in the first place was to be able to delete an entry by its 'ID'

So when i re opened the console, and added another entry, the 'ID' didnt reset back to 0

I was aiming to be able to delete an entry by its 'ID', but now thinking about it, i dont think that would work.

0 20/09/2009 Watch Clothing £29.99
1 21/02/2009 Watch Clothes £13.99
2 11/11/2009 Bangle Jewellery £674.99

This is what the data in the text file looks like, can i not delete the data from the text file directly? Overwrite it maybe?

My heads spinning lol, im new to all this
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Deleting Structured Array Entry

 
0
  #4
Apr 20th, 2009
>>can i not delete the data from the text file directly? Overwrite it maybe?

The only way to delete a line in a text file is to rewrite the entire file, leaving out the line to be deleted.

Also, don't put the £ symbol in the file because it just makes reading the file a little more difficult.

line 34 doesn't really do anything because temp is overwritten on every loop iteration. You need to add more code inside that loop to populate that array of structures as the rows are read. You can't wait until after reading is done. For example. At the end of the loop below the variable counter will contain the number of rows read from the file. You will want to use that variable throughout the rest of the program to determine how many valid entries there are in the array. When deleting a row make sure the row number to be deleted does not exceed the value of counter, which will probably be less than 100. When adding a new row you have to add it to the first unused entry (counter+1) and increment counter variable.
  1. #include <sstream> // required for stringstream class
  2.  
  3. if(myfile.is_open())
  4. {
  5. int counter = 0;
  6. while(getline(myfile,line))
  7. {
  8. stringstream str(line);
  9. // now split the line into its individual parts
  10. // These are listed in the same order as they appear
  11. // in the structure.
  12. str >> entries[counter].ID
  13. >> entries[counter].date
  14. >> entries[counter].item
  15. >> entries[counter].category
  16. >> entries[counter].price;
  17. }
  18. myfile.close();
  19. }
Last edited by Ancient Dragon; Apr 20th, 2009 at 11:50 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: bolx is an unknown quantity at this point 
Solved Threads: 0
bolx bolx is offline Offline
Newbie Poster

Re: Deleting Structured Array Entry

 
0
  #5
Apr 20th, 2009
Thanks again Dragon for the reply, ive sat and read through it a few times to get my head around it a bit more, it seems to be sinking in a little bit haha

Ive got rid of line 34 now and added the stringsteam header file

  1. int main()
  2. {
  3.  
  4. if(myfile.is_open())
  5. {
  6. int counter = 0;
  7. while(getline(myfile,line))
  8. {
  9. stringstream str(line);
  10.  
  11. str >> entries[counter].ID
  12. >> entries[counter].date
  13. >> entries[counter].item
  14. >> entries[counter].category
  15. >> entries[counter].price;
  16. }
  17. myfile.close();
  18. }
  19.  
  20. count=atoi(temp.c_str());
  21. return 0;
  22. }

Is that where i am supposed to put it?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: bolx is an unknown quantity at this point 
Solved Threads: 0
bolx bolx is offline Offline
Newbie Poster

Re: Deleting Structured Array Entry

 
0
  #6
Apr 20th, 2009
Dragon, ive decided that alot of my code was unnecessary, so i got rid of some functions, and made it alot simpler, and i also added the code that you suggested, although im not sure what it does lol

  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <fstream>
  7. #include <sstream>
  8. using namespace std;
  9.  
  10. int count;
  11. int userChoice;
  12.  
  13. struct entry{
  14. int ID;
  15. string type;
  16. string date;
  17. string item;
  18. string price;
  19. string category;
  20. }entries[100];
  21.  
  22. int newEntry();
  23. void mainMenu();
  24. void monthlyExpenditure();
  25. void deleteEntry();
  26.  
  27.  
  28. int main()
  29. {
  30. string line;
  31. ifstream myfile ("Expend.txt");
  32. if(myfile.is_open())
  33. {
  34. int count = 0;
  35. while(getline(myfile,line))
  36. {
  37. stringstream str(line);
  38.  
  39. str >> entries[count].ID
  40. >> entries[count].type
  41. >> entries[count].date
  42. >> entries[count].item
  43. >> entries[count].category
  44. >> entries[count].price;
  45. }
  46. myfile.close();
  47. }
  48.  
  49.  
  50. do{
  51. mainMenu();
  52. }while(userChoice != 1 || userChoice != 2);
  53.  
  54. return 0;
  55. }
  56.  
  57.  
  58. void mainMenu()
  59. {
  60. cout<<"Monthly Expenditure"<<endl<<endl<<endl;
  61. cout<<"Main Menu"<<endl<<endl;
  62. cout<<"Add Entry [1]"<<endl<<"Delete Entry [2]"<<endl<<"View Expenditure [3]"<<endl;
  63. cin>>userChoice;
  64.  
  65. if(userChoice == 1)
  66. {
  67. system("cls");
  68. newEntry();
  69. }
  70.  
  71. else if(userChoice == 2)
  72. {
  73. system("cls");
  74. deleteEntry();
  75. }
  76.  
  77. else if(userChoice == 3)
  78. {
  79. system("cls");
  80. monthlyExpenditure();
  81. }
  82.  
  83. else
  84. {
  85. system("cls");
  86. cout<<"Invalid input, please select again [1] [2] [3]";
  87. }
  88. }
  89.  
  90. void monthlyExpenditure()
  91. {
  92. string line;
  93. ifstream myfile ("Expend.txt");
  94. if(myfile.is_open())
  95. {
  96. while(!myfile.eof())
  97. {
  98. getline(myfile,line);
  99. cout<<line<<endl;
  100. }
  101. myfile.close();
  102. }
  103.  
  104. }
  105.  
  106. int newEntry()
  107. {
  108. cout<<"New Entry"<<endl<<endl;
  109.  
  110. entries[count].ID=count;
  111. cout<<"Type: ";
  112. cin>>entries[count].type;
  113. cout<<"Date: ";
  114. cin>>entries[count].date;
  115. cout<<"Item: ";
  116. cin>>entries[count].item;
  117. cout<<"Price: ";
  118. cin>>entries[count].price;
  119. cout<<"Category: ";
  120. cin>>entries[count].category;
  121. cout<<endl;
  122.  
  123. ofstream fout;
  124.  
  125. fout.open("Expend.txt", ios::app);
  126.  
  127. fout<<entries[count].ID<<" "<<entries[count].type<<" "<<entries[count].date<<" "<<entries[count].item<<" "<<entries[count].category<<" "<<entries[count].price<<endl;
  128. fout<<flush;
  129. fout.close();
  130.  
  131. count++;
  132.  
  133. return 0;
  134. }
  135.  
  136.  
  137. void deleteEntry()
  138. {
  139. //code here to enable to delete an entry
  140. }

I have taken out the code for the counter, should i put it back in? all it did was save the count in a text file, so when i reopened the console, it didnt reset to 0

Heres what my new text file looks like now, just 1 text file now instead of 2 for both Income and Expenses

0 Income 02/02/2009 Salary Wage 1299.99
1 Expense 02/02/2009 Watch Jewellery 29.99
2 Expense 26/02/2009 Car Vehicle 3412.87
0 Income 29/02/2009 Salary Wage 1087.00
0 Expense 29/02/2009 Sunglasses Clothing 124.65

the new entries[count].type is for the user to enter whether its a income or expense, also can you see the 'ID' down the side, there are two entries with 0, the last two entries, this is where i closed the console and reopened it, and then added another entry, the count is back to 0

Does that code you gave me load the information from the text file back into the array?
Last edited by bolx; Apr 20th, 2009 at 7:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Deleting Structured Array Entry

 
0
  #7
Apr 20th, 2009
Originally Posted by bolx View Post
and i also added the code that you suggested, although im not sure what it does lol
What it does is move the line read from the file into the structure. There is no point reading the file if you are not going to do anything with the data.

Originally Posted by bolx View Post
I have taken out the code for the counter, should i put it back in?
No -- variable count is ok too.

Originally Posted by bolx View Post
all it did was save the count in a text file, so when i reopened the console, it didnt reset to 0
Actually saving count to the text file. Just count the lines as they are read is all that you need to do.

And you need to increment the value of count on line 44, which I think I forgot to do in my post.

Originally Posted by bolx View Post
Does that code you gave me load the information from the text file back into the array?
Yes
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: bolx is an unknown quantity at this point 
Solved Threads: 0
bolx bolx is offline Offline
Newbie Poster

Re: Deleting Structured Array Entry

 
0
  #8
Apr 21st, 2009
Cheers again Dragon for the help, i tried using the string stream to load the data back into the array, but it didnt work, so instead i tried this

  1.  
  2. ifstream fin ("Expend.txt");
  3. if(fin.is_open())
  4. {
  5.  
  6. while (!fin.eof())
  7. {
  8. for (j=1;j<250;j++)
  9. {
  10. fin >> entries[j].ID
  11. >> entries[j].type
  12. >> entries[j].date
  13. >> entries[j].item
  14. >> entries[j].price
  15. >> entries[j].category;
  16.  
  17. }
  18. }
  19. fin.close();
  20. }

This seems to work too, i need to pick your brains about somethin else also. Is there such code that will close the console window, i want an option on the menu that when a user enters that option, it will exit the console

And another thing, am i able to save all the data in the structure array to a text file, in one go. Pls can you show me some example code of how i can achieve this. I have an idea, i think i need to put it in a loop
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: bolx is an unknown quantity at this point 
Solved Threads: 0
bolx bolx is offline Offline
Newbie Poster

Re: Deleting Structured Array Entry

 
0
  #9
Apr 21st, 2009
  1. ofstream fout;
  2.  
  3. fout.open("Expend.txt");
  4.  
  5. for (j=1;j<250;j++)
  6. {
  7.  
  8. fout
  9. <<entries[j].type<<" "
  10. <<entries[j].date<<" "
  11. <<entries[j].item<<" "
  12. <<entries[j].price<<" "
  13. <<entries[j].category<<endl;
  14.  
  15.  
  16. }
  17. fout<<flush;
  18. fout.close();

I tried this, but it saves all 250 array entries as 0, i dont know how to get it to just saved the entries with data in them
Last edited by bolx; Apr 21st, 2009 at 2:38 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC