| | |
How do I delete an instance of an Array?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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)
#include <iostream> #include <iomanip> #include <string> #define MAX 100 //Maximum number of books in inventory array using namespace std; class CBooks //Class definition { public: //Constructor CBooks(){string m_bookName; string m_author; string m_isbn;} //Destructor ~CBooks(){}; //Functions void inputBooks(void); void displayBooks(void); void getProfit(void); void viewProfit(void); private: string m_bookName; //Name of book string m_author; //Author of book string m_isbn; //ISBN of book float m_price; //Price of book float m_profit; //Profit of book sale float m_totalProfit; //Total of all profits int m_choice; //Number of book chosen for purchase }inventory[MAX]; //Array of class objects //Global variables int current; //Used to count number of books in inventory //Main Function int main() { //Declare variables int x = 0, transaction, another = 2; string password; while (another != 1) { system("CLS"); //display section header cout << "***********************\n"; cout << "*Team A Book Inventory*\n"; cout << "***********************\n\n"; cout << "Please type the number of the desired action and press <ENTER>:\n" ; cout << "1. Buy\n"; cout << "2. Sell\n"; cout << "3. View Profit\n"; cin >> transaction; cin.ignore(); //statement is necessary in order to make cin.getline() to work properly. //Tells cin to ignore the line feed at the end of input //code to view profit if (transaction == 3) { inventory[x].viewProfit(); } // code to sell a textbook if (transaction == 2) { if (x<=99) { system("CLS"); //display section header cout << "***********\n"; cout << "*Sell Book*\n"; cout << "***********\n\n"; inventory[x].inputBooks(); x++; } else { cout << "I'm sorry, the database cannot hold any more books.\n"; //print if more then 100 books in inventory } } //code to buy book if (transaction == 1) { system("CLS"); //clear screen //display section header cout << "**********\n"; cout << "*Buy Book*\n"; cout << "**********\n\n"; cout << "Book Name " << "Book Author " << "ISBN " << "Price\n"; //print buy header cout << "--------- " << "----------- " << "---- " << "-----\n\n"; //print buy header inventory[x].displayBooks(); } cout << "\nPlease type the number of the desired action and press <ENTER>:\n"; //print option make another trans cout << "1. Quit program\n"; //quit program cout << "2. Make another transaction\n"; //make another trans cin >> another; //get answer if (another == 1) //check if user selected exit { cout << "WARNING!! Data will be lost if program is exited. Are you sure you want to exit?\n"; //print warning about loosing data cout << "1. Yes, please exit.\n"; cout << "2. No, stay in program.\n"; cin >> another; //get answer again } } return 0; } //Input books into inventory function void CBooks::inputBooks(void) { cout << "Please enter the book's name: \n"; getline(cin, m_bookName); cout << "Please enter the book's author: \n"; getline(cin, m_author); cout << "Please enter the book's ISBN: \n"; getline(cin, m_isbn); cout << "Please enter the desired price: \n"; cin >> m_price; cin.ignore(); //Increments the number of books entered into inventory current++; cout << "\nThere are now " << current << " books for in our inventory.\n"; } //Diplay books in inventory function void CBooks::displayBooks(void) { int loop; for (loop = 0; loop < current; loop++) { cout << inventory[loop].m_bookName << " "; cout << inventory[loop].m_author << " "; cout << inventory[loop].m_isbn << " "; cout << inventory[loop].m_price << "\n"; } cout << "\nPlease enter the number of the book you would like to buy:\n"; cin >> m_choice; //the number choosen cout << "Thank you for your purchase!\n\n"; //Calculates 10% profit and updates total profit m_profit = inventory[m_choice - 1].m_price * .10; m_totalProfit += m_profit; } //Checks password and diplays profit void CBooks::viewProfit() { string password; system("CLS"); //display header section cout << "*************\n"; cout << "*View Profit*\n"; cout << "*************\n\n"; cout << "Please type your password:\n"; cin >> password; if (password == "upwd") //check password { cout << "Profit = $" << fixed << showpoint << setprecision(2) << m_totalProfit << "\n"; } else { cout << "Invalid password.\n"; } system("PAUSE"); }
Last edited by moznmar; Jan 16th, 2006 at 4:12 pm. Reason: Posted in wrong forum
>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.
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
•
•
•
•
Originally Posted by Narue
>In the future, you can either delete your thread with the thread tools provided nobody has posted a reply
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
![]() |
Similar Threads
- remove item in array (C++)
- Resizing of an array. (C++)
- Delete an item from an array (C)
- Problem on array based list (C)
- works for static need help to make it dynamic (C++)
- Can't delete virus (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Global Variables
- Next Thread: Passing variables around in a MFC project
Views: 3057 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






