How do I delete an instance of an Array?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 12
Reputation: moznmar is an unknown quantity at this point 
Solved Threads: 0
moznmar's Avatar
moznmar moznmar is offline Offline
Newbie Poster

How do I delete an instance of an Array?

 
0
  #1
Jan 16th, 2006
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,859
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: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #2
Jan 16th, 2006
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 12
Reputation: moznmar is an unknown quantity at this point 
Solved Threads: 0
moznmar's Avatar
moznmar moznmar is offline Offline
Newbie Poster

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

 
0
  #3
Jan 16th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

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

 
0
  #4
Jan 16th, 2006
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.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3057 | Replies: 3
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC