| | |
Urgent:need Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 1
Reputation:
Solved Threads: 0
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
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)
#include<iostream.h> #include<conio.h> #include<fstream.h> class inventory { char b_name[15]; char p_name[15]; float isbn; int pur; float p_cost; float s_cost; int hand; public: void getdata() { cout<<"ENTER ISBN :";cin>>isbn; cout<<"ENTER BOOK :";cin>>b_name; cout<<"ENTER PUBLSHER :";cin>>p_name; cout<<"ENTER PURCHASE COST :";cin>>p_cost; cout<<"ENTER SALE COST :";cin>>s_cost; } void inhand() { cout<<"ENTER QUANTITY PURCHASED :";cin>>pur; hand=pur; } void sale() { hand=hand-1; } void show() { cout<<"\nISBN :"<<isbn<<"\nBOOK :"<<b_name<<"\nPUBLISHER :"<<p_name; cout<<"\nQUANTITY PURCHASE :"<<pur<<"\nQUANTITY IN HAND :"<<hand; cout<<"\nPURCHASE COST :"<<p_cost<<"\nSALE COST :"<<s_cost; } }; void main() { clrscr(); char c; inventory inv; fstream file; file.open("inv_dat.txt",ios::ate); cout<<"\n\n\n\t\t\"MENU\""; cout<<"\nPRESS \"1\" to print all the records."; cout<<"\nPRESS \"2\" to delete a record."; cout<<"\nPRESS \"3\" to update a record."; cout<<"\nPRESS \"4\" to enter a new record."; cout<<"\nPRESS \"5\" to sell a book."; cout<<"\nPRESS \"5\" to calculate profit in this month."; cout<<"\nPRESS \"0\" for EXIT"; char choice='0'; cout<<"\n\n\"ENTER CHOICE\" :";cin>>choice; switch (choice) { case'1': { file.open("inv_dat.txt",ios::in); file.seekg(0); file.read((char*)&inv,sizeof(inv)); do { inv.show(); cout<<endl; file.read((char*)&inv,sizeof(inv)); } while(!file.eof()); getch(); };break; case '2': { file.open("inv_dat.txt",ios::in|ios::out|ios::ate); file.seekg(0,ios::end); int n,pos; cout<<"The records present are :"<<file.tellg()/sizeof(inv); cout<<"\nEnter ISBN no. of the record you want to delete :"; cin>>n; pos=(n-1) *sizeof(inv); file.seekp(pos); file.write((char *)&inv,sizeof(inv)); getch(); };break; case '3': { file.open("inv_dat.txt",ios::in|ios::out|ios::ate); int n,pos; cout<<"Enter ISBN no. of the record you want to update"; cin>>n; pos=(n-1) *sizeof(inv); file.seekp(pos); cout<<"\n\" ENTER NEW DATA \"\n"; inv.getdata(); inv.inhand(); file.write((char *)&inv,sizeof(inv)); file<<flush; getch(); };break; case '4': { file.open("inv_dat.txt",ios::app); do { inv.getdata(); inv.inhand(); file.write((char*)&inv,sizeof(inv)); cout<<"\n\nWnat to enter another object(y/n):" ;cin>>c; } while (c=='y'); };break; case '5': { file.open("inv_dat.txt",ios::in|ios::out); int n,pos; cout<<"\n Enter the ISBN no of the Book you want to sell :"; cin>>n; pos=(n-1) *sizeof(inv); file.seekp(pos); file.read((char*)&inv,sizeof(inv)); inv.sale(); file.write((char*)&inv,sizeof(inv)); file<<flush; getch(); };break; default:getch(); } }
Last edited by alc6379; Dec 27th, 2004 at 11:15 am. Reason: added [code] tags
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.
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.
•
•
•
•
you might have to COPY the file over to a new file, minus this record, and then delete the original and rename the copy.
bit of sample code:
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < number_of_records; i++) { if(i == record_number_i_want_to_delete) continue; // this skips the rest of the loop and effectively goes back to the top // copy record[i] or whatever... }
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- pls heeeeeeeeelp its urgent. (C)
- Urgent! (C++)
- URGENT: Need help on I/O code! (Java)
- Urgent help (Networking Hardware Configuration)
- Homepage keeps switching back to a porn site (Web Browsers)
- Importing SQL Script File - Urgent !! (Database Design)
Other Threads in the C++ Forum
- Previous Thread: I have an exam, please give me a site!
- Next Thread: Problems with character strings
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






