random-access files

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

random-access files

 
0
  #1
Sep 21st, 2005
Hi, I'm doing this programming exercise for my C++ class. I'm using Microsoft Visual C++. for some reason, the program wouldn't save my data. Can someone please take a look at my code and tell me what I did wrong?

  1.  
  2. /*
  3. Chapter 12 HOMEWORK
  4. Dung Tran
  5. CS 116 C++ programming
  6. Chapter 12 Programming Exercise #11 Inventory Program
  7. Requirements:
  8. Write a program that uses a structure to store these datas in a file:
  9. - item Description
  10. - Quantity on hand
  11. - wholesale cost
  12. - retail cost
  13. The program should have a menu that allows the usess to perform the following tasks:
  14. - add new records to the file
  15. - display any record in the file
  16. - change any record in the file
  17. */
  18. #include <iostream>
  19. #include <fstream>
  20. using namespace std;
  21.  
  22. // Declaration of InventoryItem structure
  23. struct InventoryItem
  24. {
  25. char desc[51]; //item description
  26. int qty; //quantity on hand
  27. float wholesale; //wholesale price
  28. float retail; //retail price
  29. };
  30.  
  31. //Function Prototypes
  32. int menu();
  33. void AddRecord(fstream &);
  34. void DisplayRecord(fstream &);
  35. void ChangeRecord(fstream &);
  36.  
  37. int main()
  38. {
  39. int choice;
  40. fstream inventory ("Inventory.dat", ios::out | ios::binary);
  41. InventoryItem record = {" ", 0, 0.0};
  42.  
  43. //writing the blank records
  44. for (int count = 0; count < 5; count++)
  45. {
  46. inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
  47. }
  48.  
  49. //inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
  50.  
  51. inventory.close();
  52.  
  53. inventory.open("Inventory.dat", ios::out | ios::binary);
  54.  
  55. do
  56. {
  57. choice = menu();
  58. switch (choice)
  59. {
  60. case 1: AddRecord(inventory);
  61. break;
  62. case 2: DisplayRecord(inventory);
  63. break;
  64. case 3: ChangeRecord(inventory);
  65. break;
  66. case 4: cout << "Exiting Program...\n\n";
  67. }
  68. }while (choice != 4);
  69.  
  70. inventory.close();
  71. return 0;
  72.  
  73. }
  74.  
  75. int menu ()
  76. {
  77. int selection;
  78.  
  79. cout << "What would you like to do?\n";
  80. cout << "1) add a new record to the file\n";
  81. cout << "2) view a record in the file\n";
  82. cout << "3) change a record in the file\n";
  83. cout << "4) Exit Program\n\b";
  84. cout << "Please enter 1, 2, 3, or 4: ";
  85. cin >> selection;
  86.  
  87. while (selection< 1 || selection > 4)
  88. {
  89. cout << "Invalid Choice!!\n";
  90. cin >> selection;
  91. }
  92. return selection;
  93. }
  94.  
  95. void ChangeRecord(fstream &file)
  96. {
  97. fstream inventory ("Inventory.dat", ios::out | ios::binary);
  98. InventoryItem record;
  99. long recNum;
  100.  
  101. // Get the record number of the desired record.
  102. cout << "Which record do you want to edit? ";
  103. cin >> recNum;
  104.  
  105. // Move to the record and read it.
  106. inventory.seekg(recNum * sizeof(record), ios::beg);
  107. inventory.read(reinterpret_cast<char *>(&record),
  108. sizeof(record));
  109.  
  110. // Display the record contents
  111. cout << "Description: ";
  112. cout << record.desc << endl;
  113. cout << "Quantity: ";
  114. cout << record.qty << endl;
  115. cout << "Wholesale Price: ";
  116. cout << record.wholesale << endl;
  117. cout << "Retail Price: ";
  118. cout << record.retail << endl;
  119.  
  120. // Get the new record data.
  121. cout << "Enter the new data:\n";
  122. cout << "Description: ";
  123. cin.ignore();
  124. cin.getline(record.desc, 31);
  125. cout << "Quantity: ";
  126. cin >> record.qty;
  127. cout << "Wholesale Price: ";
  128. cin >> record.wholesale;
  129. cout << "Retail Price: ";
  130. cin >> record.retail;
  131.  
  132. // Move back to the beginning of this record's position.
  133. inventory.seekp(recNum * sizeof(record), ios::beg);
  134.  
  135. // Write the new record over the current record.
  136. inventory.write(reinterpret_cast<char *>(&record),
  137. sizeof(record));
  138.  
  139. // Close the file.
  140. inventory.close();
  141. }
  142.  
  143. void DisplayRecord(fstream &file)
  144. {
  145. fstream inventory ("Inventory.dat", ios::out | ios::binary);
  146. InventoryItem record;
  147. long recNum;
  148.  
  149. // Get the record number of the desired record.
  150. cout << "Which record would you like to open? ";
  151. cin >> recNum;
  152.  
  153. // Move to the record and read it.
  154. inventory.seekg(recNum * sizeof(record), ios::beg);
  155. inventory.read(reinterpret_cast<char *>(&record),
  156. sizeof(record));
  157.  
  158. // Display the record contents
  159. cout << "Description: ";
  160. cout << record.desc << endl;
  161. cout << "Quantity: ";
  162. cout << record.qty << endl;
  163. cout << "Wholesale Price: ";
  164. cout << record.wholesale << endl;
  165. cout << "Retail Price: ";
  166. cout << record.retail << endl;
  167.  
  168. //clear any error state
  169. if (file.fail())
  170. file.clear();
  171.  
  172. //closing the file
  173. file.close();
  174. }
  175.  
  176. void AddRecord(fstream &file)
  177. {
  178. cout << "Please enter the information for the new data: \n";
  179.  
  180. fstream inventory ("Inventory.dat", ios::out | ios::binary);
  181. InventoryItem record;
  182.  
  183. //Info of the new data
  184. cout << "Description: ";
  185. cin.ignore();
  186. cin.getline(record.desc, 31);
  187. cout << "Quantity: ";
  188. cin >> record.qty;
  189. cout << "Wholesale Price: ";
  190. cin >> record.wholesale;
  191. cout << "Retail Price: ";
  192. cin >> record.retail;
  193.  
  194. inventory.write(reinterpret_cast<char *>(&record),
  195. sizeof(record));
  196.  
  197. //closing the file
  198. file.close();
  199. }
Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: random-access files

 
0
  #2
Sep 22nd, 2005
Nice to hear from you again. Looks like you've really improved!

Unfortunately, my schedule is much tighter these days, so I don't have the time to help you out explicitly, but perhaps this may help...

http://cplus.about.com/od/beginnerct.../aa051802a.htm

It's a simple tutorial for file I/O.
I hope it's helpful.
Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: random-access files

 
0
  #3
Sep 23rd, 2005
Originally Posted by Drowzee
Nice to hear from you again. Looks like you've really improved!

Unfortunately, my schedule is much tighter these days, so I don't have the time to help you out explicitly, but perhaps this may help...

http://cplus.about.com/od/beginnerct.../aa051802a.htm

It's a simple tutorial for file I/O.
I hope it's helpful.
WOW, you still remember me?!! :surprised I mean, there are tons of people on this website! :surprised . LOL. I guess I did ask a lot of questions.

It's nice of you to follow my progress. I'd hope that I'm better now. But I'm still only in 2nd semester of C++. Still have much to learn.

Anyway, thanks for the link. I just figured out my coding mistakes. But I'll take a look at the site. Attaining more knowledge is always good.

Thanks again
Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: smitem03 is an unknown quantity at this point 
Solved Threads: 0
smitem03 smitem03 is offline Offline
Newbie Poster

Help!

 
0
  #4
Apr 15th, 2009
I have something like you that I am doing....What was wrong with your code???

Thanks
smi
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: random-access files

 
1
  #5
Apr 15th, 2009
I don't think your question will get an answer seeing as how karen_CSE hasn't been active since 2006.
New members chased away this month: 3
Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: smitem03 is an unknown quantity at this point 
Solved Threads: 0
smitem03 smitem03 is offline Offline
Newbie Poster

Re: random-access files

 
0
  #6
Apr 15th, 2009
Well thanks... Could you tell me what is wrong? We are using a more basic C ++ language then shown above. Could you tell me what you think is wrong?
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: random-access files

 
1
  #7
Apr 15th, 2009
I have a better idea. Why don't you start a new thread, post your code, and ask a question specific to your problem. That's more likely to result in a useful answer to you.
New members chased away this month: 3
Quick reply to this message  
Closed Thread

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



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

©2003 - 2009 DaniWeb® LLC