Urgent:need Help

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

Join Date: Dec 2004
Posts: 1
Reputation: sa-rulz is an unknown quantity at this point 
Solved Threads: 0
sa-rulz sa-rulz is offline Offline
Newbie Poster

Urgent:need Help

 
0
  #1
Dec 27th, 2004
Hi
I have been given a assignment of making a "inventory control program of a book store in c++" i have done all the parts except for two

1. I have to delete a record from the file (i am using fstream and saving the file i need the piece of code to delete the particular record).

2 . i have to sell a book but when ever i subtract something from the record a new record gets created and the problem becomes harder.

this the program

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<fstream.h>
  4.  
  5. class inventory
  6. {
  7. char b_name[15];
  8. char p_name[15];
  9. float isbn;
  10. int pur;
  11. float p_cost;
  12. float s_cost;
  13. int hand;
  14. public:
  15.  
  16. void getdata()
  17. {
  18. cout<<"ENTER ISBN :";cin>>isbn;
  19. cout<<"ENTER BOOK :";cin>>b_name;
  20. cout<<"ENTER PUBLSHER :";cin>>p_name;
  21. cout<<"ENTER PURCHASE COST :";cin>>p_cost;
  22. cout<<"ENTER SALE COST :";cin>>s_cost;
  23. }
  24.  
  25. void inhand()
  26. {
  27. cout<<"ENTER QUANTITY PURCHASED :";cin>>pur;
  28. hand=pur;
  29. }
  30.  
  31. void sale()
  32. {
  33. hand=hand-1;
  34. }
  35.  
  36. void show()
  37. {
  38. cout<<"\nISBN :"<<isbn<<"\nBOOK :"<<b_name<<"\nPUBLISHER :"<<p_name;
  39. cout<<"\nQUANTITY PURCHASE :"<<pur<<"\nQUANTITY IN HAND :"<<hand;
  40. cout<<"\nPURCHASE COST :"<<p_cost<<"\nSALE COST :"<<s_cost;
  41. }
  42. };
  43.  
  44. void main()
  45. {
  46. clrscr();
  47. char c;
  48. inventory inv;
  49. fstream file;
  50. file.open("inv_dat.txt",ios::ate);
  51. cout<<"\n\n\n\t\t\"MENU\"";
  52. cout<<"\nPRESS \"1\" to print all the records.";
  53. cout<<"\nPRESS \"2\" to delete a record.";
  54. cout<<"\nPRESS \"3\" to update a record.";
  55. cout<<"\nPRESS \"4\" to enter a new record.";
  56. cout<<"\nPRESS \"5\" to sell a book.";
  57. cout<<"\nPRESS \"5\" to calculate profit in this month.";
  58. cout<<"\nPRESS \"0\" for EXIT";
  59. char choice='0';
  60. cout<<"\n\n\"ENTER CHOICE\" :";cin>>choice;
  61. switch (choice)
  62. {
  63. case'1':
  64. {
  65. file.open("inv_dat.txt",ios::in);
  66. file.seekg(0);
  67. file.read((char*)&inv,sizeof(inv));
  68. do
  69. {
  70. inv.show();
  71. cout<<endl;
  72. file.read((char*)&inv,sizeof(inv));
  73. }
  74. while(!file.eof());
  75. getch();
  76. };break;
  77. case '2':
  78. {
  79. file.open("inv_dat.txt",ios::in|ios::out|ios::ate);
  80. file.seekg(0,ios::end);
  81. int n,pos;
  82. cout<<"The records present are :"<<file.tellg()/sizeof(inv);
  83. cout<<"\nEnter ISBN no. of the record you want to delete :";
  84. cin>>n;
  85. pos=(n-1) *sizeof(inv);
  86. file.seekp(pos);
  87. file.write((char *)&inv,sizeof(inv));
  88. getch();
  89. };break;
  90. case '3':
  91. {
  92. file.open("inv_dat.txt",ios::in|ios::out|ios::ate);
  93. int n,pos;
  94. cout<<"Enter ISBN no. of the record you want to update";
  95. cin>>n;
  96. pos=(n-1) *sizeof(inv);
  97. file.seekp(pos);
  98. cout<<"\n\" ENTER NEW DATA \"\n";
  99. inv.getdata();
  100. inv.inhand();
  101. file.write((char *)&inv,sizeof(inv));
  102. file<<flush;
  103. getch();
  104. };break;
  105. case '4':
  106. {
  107. file.open("inv_dat.txt",ios::app);
  108.  
  109. do
  110. {
  111. inv.getdata();
  112. inv.inhand();
  113. file.write((char*)&inv,sizeof(inv));
  114. cout<<"\n\nWnat to enter another object(y/n):" ;cin>>c;
  115. }
  116. while (c=='y');
  117. };break;
  118. case '5':
  119. {
  120. file.open("inv_dat.txt",ios::in|ios::out);
  121. int n,pos;
  122. cout<<"\n Enter the ISBN no of the Book you want to sell :";
  123. cin>>n;
  124. pos=(n-1) *sizeof(inv);
  125. file.seekp(pos);
  126. file.read((char*)&inv,sizeof(inv));
  127. inv.sale();
  128. file.write((char*)&inv,sizeof(inv));
  129. file<<flush;
  130. getch();
  131. };break;
  132.  
  133.  
  134. default:getch();
  135. }
  136. }
Last edited by alc6379; Dec 27th, 2004 at 11:15 am. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Urgent:need Help

 
0
  #2
Dec 27th, 2004
please don't name your threads like this anymore.

Urgent::need help is just plain rude. It implies your thread is more important than others, when this forum prioritizes help requests on a first come, first-served basis.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Urgent:need Help

 
0
  #3
Dec 27th, 2004
On the 'new record' issue, everywhere else you use the ISBN as an index into the file. Yet when you write a new record, you have opened the file in 'append' mode so the write happens at the end of the file. Unless you add records in ISBN order, this won't work.

Speaking of ISBN, that's some number assigned by the publisher, isn't it? For file positions you probably want a number that is generally in the range 0..n so you don't have file waste. If ISBN's are like '17750294' you are going to have a massive file with a ton of dead space in it if you use the ISBN as a record number.

Alternatives would be to read the entire file to find the entered ISBN, or to maintain an array in RAM of the ISBN's in the file with their corresponding file position or record index. You would read the file once at startup to build this in-ram list and then maintain it as you go. OR you could have a second file with ISBN's in it that might be faster to read than the entire book file. Getting more sophisticated, you could investigate an indexing method like BTrees (look it up in Google) and build a 'sorted' index file.

Since the records are in this specific order, though, a 'delete' could be as simple as setting the quantity on hand to 0 or you could write nulls/spaces into the record, or if the record needs to be removed entirely, you might have to COPY the file over to a new file, minus this record, and then delete the original and rename the copy.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Urgent:need Help

 
0
  #4
Dec 27th, 2004
you might have to COPY the file over to a new file, minus this record, and then delete the original and rename the copy.
extremly wise words. i cant remember how many times i have told people the same advice! this is better than setting zeros and its VERY EASY to implement. got the reconrd number you want to delete and use a loop to copy records with an IF statement to trap the record number to delete (which doesnt get copied)

bit of sample code:
  1. for(int i = 0; i < number_of_records; i++)
  2. {
  3. if(i == record_number_i_want_to_delete)
  4. continue; // this skips the rest of the loop and effectively goes back to the top
  5. // copy record[i] or whatever...
  6. }
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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