943,962 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2186
  • C++ RSS
Dec 27th, 2004
0

Urgent:need Help

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sa-rulz is offline Offline
1 posts
since Dec 2004
Dec 27th, 2004
0

Re: Urgent:need Help

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Dec 27th, 2004
0

Re: Urgent:need Help

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Dec 27th, 2004
0

Re: Urgent:need Help

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I have an exam, please give me a site!
Next Thread in C++ Forum Timeline: Problems with character strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC