943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11396
  • C++ RSS
Sep 21st, 2005
0

random-access files

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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Sep 22nd, 2005
0

Re: random-access files

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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Sep 23rd, 2005
0

Re: random-access files

Quote 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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Apr 15th, 2009
0

Help!

I have something like you that I am doing....What was wrong with your code???

Thanks
smi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smitem03 is offline Offline
8 posts
since Apr 2009
Apr 15th, 2009
1

Re: random-access files

I don't think your question will get an answer seeing as how karen_CSE hasn't been active since 2006.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 15th, 2009
0

Re: random-access files

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smitem03 is offline Offline
8 posts
since Apr 2009
Apr 15th, 2009
1

Re: random-access files

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: access to variables
Next Thread in C++ Forum Timeline: My First Multi-Threaded Code! What Do You Think?





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


Follow us on Twitter


© 2011 DaniWeb® LLC