954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I delete an instance of an Array?

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.

#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");
}
moznmar
Newbie Poster
12 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

moznmar
Newbie Poster
12 posts since Jun 2004
Reputation Points: 10
Solved Threads: 0
 
>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.

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You