944,016 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3693
  • C++ RSS
Jan 16th, 2006
0

How do I delete an instance of an Array?

Expand Post »
I apologize, but I meant to make the below post in the C/C++ forum. I'm not sure how I may move it to the other forum or delete it and repost it in the correct forum. I would appreciate any guidance on how to correct my mistake. Thank you.

I'm using an array of class instances to store an inventory of a book store program I'm writing for school. Upon selling a book, I would like to have the instance storing the data on the sold book deleted out so that the book won't show the next time I display the inventory. I've been working on this for a while, but seem to be stuck. Any ideas? Here is the code I've written so far.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #define MAX 100 //Maximum number of books in inventory array
  5.  
  6. using namespace std;
  7.  
  8. class CBooks //Class definition
  9. {
  10. public:
  11.  
  12. //Constructor
  13. CBooks(){string m_bookName; string m_author; string m_isbn;}
  14.  
  15. //Destructor
  16. ~CBooks(){};
  17.  
  18. //Functions
  19. void inputBooks(void);
  20. void displayBooks(void);
  21. void getProfit(void);
  22. void viewProfit(void);
  23.  
  24. private:
  25. string m_bookName; //Name of book
  26. string m_author; //Author of book
  27. string m_isbn; //ISBN of book
  28. float m_price; //Price of book
  29. float m_profit; //Profit of book sale
  30. float m_totalProfit; //Total of all profits
  31. int m_choice; //Number of book chosen for purchase
  32. }inventory[MAX]; //Array of class objects
  33.  
  34. //Global variables
  35. int current; //Used to count number of books in inventory
  36.  
  37. //Main Function
  38. int main()
  39. {
  40. //Declare variables
  41. int x = 0, transaction, another = 2;
  42. string password;
  43.  
  44. while (another != 1)
  45. {
  46. system("CLS");
  47. //display section header
  48. cout << "***********************\n";
  49. cout << "*Team A Book Inventory*\n";
  50. cout << "***********************\n\n";
  51.  
  52. cout << "Please type the number of the desired action and press <ENTER>:\n" ;
  53. cout << "1. Buy\n";
  54. cout << "2. Sell\n";
  55. cout << "3. View Profit\n";
  56. cin >> transaction;
  57. cin.ignore(); //statement is necessary in order to make cin.getline() to work properly.
  58. //Tells cin to ignore the line feed at the end of input
  59.  
  60. //code to view profit
  61. if (transaction == 3)
  62. {
  63. inventory[x].viewProfit();
  64. }
  65. // code to sell a textbook
  66. if (transaction == 2)
  67. {
  68. if (x<=99)
  69. {
  70. system("CLS");
  71. //display section header
  72. cout << "***********\n";
  73. cout << "*Sell Book*\n";
  74. cout << "***********\n\n";
  75.  
  76. inventory[x].inputBooks();
  77. x++;
  78. }
  79. else
  80. {
  81. cout << "I'm sorry, the database cannot hold any more books.\n"; //print if more then 100 books in inventory
  82. }
  83. }
  84. //code to buy book
  85. if (transaction == 1)
  86. {
  87. system("CLS"); //clear screen
  88. //display section header
  89. cout << "**********\n";
  90. cout << "*Buy Book*\n";
  91. cout << "**********\n\n";
  92.  
  93. cout << "Book Name " << "Book Author " << "ISBN " << "Price\n"; //print buy header
  94. cout << "--------- " << "----------- " << "---- " << "-----\n\n"; //print buy header
  95. inventory[x].displayBooks();
  96. }
  97. cout << "\nPlease type the number of the desired action and press <ENTER>:\n"; //print option make another trans
  98. cout << "1. Quit program\n"; //quit program
  99. cout << "2. Make another transaction\n"; //make another trans
  100. cin >> another; //get answer
  101.  
  102. if (another == 1) //check if user selected exit
  103. {
  104. cout << "WARNING!! Data will be lost if program is exited. Are you sure you want to exit?\n"; //print warning about loosing data
  105. cout << "1. Yes, please exit.\n";
  106. cout << "2. No, stay in program.\n";
  107. cin >> another; //get answer again
  108. }
  109. }
  110.  
  111. return 0;
  112. }
  113.  
  114. //Input books into inventory function
  115. void CBooks::inputBooks(void)
  116. {
  117. cout << "Please enter the book's name: \n";
  118. getline(cin, m_bookName);
  119. cout << "Please enter the book's author: \n";
  120. getline(cin, m_author);
  121. cout << "Please enter the book's ISBN: \n";
  122. getline(cin, m_isbn);
  123. cout << "Please enter the desired price: \n";
  124. cin >> m_price;
  125. cin.ignore();
  126.  
  127. //Increments the number of books entered into inventory
  128. current++;
  129. cout << "\nThere are now " << current << " books for in our inventory.\n";
  130. }
  131.  
  132. //Diplay books in inventory function
  133. void CBooks::displayBooks(void)
  134. {
  135. int loop;
  136.  
  137. for (loop = 0; loop < current; loop++)
  138. {
  139. cout << inventory[loop].m_bookName << " ";
  140. cout << inventory[loop].m_author << " ";
  141. cout << inventory[loop].m_isbn << " ";
  142. cout << inventory[loop].m_price << "\n";
  143. }
  144. cout << "\nPlease enter the number of the book you would like to buy:\n";
  145. cin >> m_choice; //the number choosen
  146. cout << "Thank you for your purchase!\n\n";
  147.  
  148. //Calculates 10% profit and updates total profit
  149. m_profit = inventory[m_choice - 1].m_price * .10;
  150. m_totalProfit += m_profit;
  151. }
  152.  
  153. //Checks password and diplays profit
  154. void CBooks::viewProfit()
  155. {
  156. string password;
  157.  
  158. system("CLS");
  159. //display header section
  160. cout << "*************\n";
  161. cout << "*View Profit*\n";
  162. cout << "*************\n\n";
  163.  
  164. cout << "Please type your password:\n";
  165. cin >> password;
  166. if (password == "upwd") //check password
  167. {
  168. cout << "Profit = $" << fixed << showpoint << setprecision(2)
  169. << m_totalProfit << "\n";
  170. }
  171. else
  172. {
  173. cout << "Invalid password.\n";
  174. }
  175. system("PAUSE");
  176. }
Last edited by moznmar; Jan 16th, 2006 at 4:12 pm. Reason: Posted in wrong forum
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moznmar is offline Offline
12 posts
since Jun 2004
Jan 16th, 2006
0

Re: How do I delete an instance of an Array?

>I'm not sure how I may move it to the other forum
Done. In the future, you can either delete your thread with the thread tools provided nobody has posted a reply, or you can contact a moderator with admin powers on both the to and from forum (such as myself) and they can move it for you.

>Any ideas?
Rather than storing objects, store pointers to objects so that a non-existent book can be a null pointer. Alternatively, use an std::list to store the books so that they can be easily removed when needed. I wouldn't recommend an array because it's just too awkward to delete items from anywhere except the end.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 16th, 2006
0

Re: How do I delete an instance of an Array?

Narue,

I appreciate you moving the post for me. I must use an array, because that is what my professor told us to use for this program. I'll try the pointers. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
moznmar is offline Offline
12 posts
since Jun 2004
Jan 16th, 2006
0

Re: How do I delete an instance of an Array?

Quote originally posted by Narue ...
>In the future, you can either delete your thread with the thread tools provided nobody has posted a reply
I actually don't believe that members have the ability to delete threads.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002

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: Global Variables
Next Thread in C++ Forum Timeline: Passing variables around in a MFC project





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


Follow us on Twitter


© 2011 DaniWeb® LLC