Hello, I am trying to create a shop where the user can pick an item to add to their cart and after they pick the item, I want to remove it from the list, but I can't seem to remember on how to do it.

int main( void )
{
	const int nSize = 5;

	CWeapons Weapons[nSize];

	// Set each Weapons w/ unique names 
	Weapons[0].Set( "Batarang", 59.99f, 1);
	Weapons[1].Set( "KeyBlade", 39.99f, 1);
	Weapons[2].Set( "Katana", 39.99f, 1);
	Weapons[3].Set( "Flail", 39.99f, 1);
	Weapons[4].Set( "Assult Rifle", 39.99f, 1);
	

	const int nCartSize = 5;
	
	int nUser;

	CQueue Queue(nCartSize);

	// shopping menu
	for(;;)
	{
		cout << "1) Add an Item to the cart\n"
			<< "2) Check Out\n"
			<< "What would you like to do:";
		if(cin >> nUser && nUser <= 2 && nUser >=1 )
		{
			cin.ignore(INT_MAX, '\n');
			break;
		}
		cin.clear();
		cin.ignore(INT_MAX, '\n');
	}


	// if user selects add items to the cart
	if(1 == nUser)
	{
		int nCheckOut;

		for(;;)
		{

			// show selextion of 5 Weapons
			for(int i = 0; i < nCartSize; ++i)
			{
				cout << i+1 << ") " << Weapons[i].GetName() << " - " << Weapons[i].GetCost() << '\n';
			}
			cout << "0) Check Out Early\n";

			// ask user what option they want to do (0-5)
			for(;;)
			{
				cout << "What weapon would you like to add: ";
				if(cin >> nCheckOut && nCheckOut >=0 && nCheckOut <=5 )
				{
					cin.ignore(INT_MAX, '\n');
					break;
				}
				cin.clear();
				cin.ignore(INT_MAX, '\n');
			}

			if(nCheckOut == 1)
			{
				Queue.Enqueue(Weapons[0]);
		
			}
			else if(nCheckOut == 2)
			{
				Queue.Enqueue(Weapons[1]);	
				
			}
			else if(nCheckOut == 3)
			{
				Queue.Enqueue(Weapons[2]);
				
			}
			else if(nCheckOut == 4)
			{
				Queue.Enqueue(Weapons[3]);
				
			}
			else if(nCheckOut == 5)
			{
				Queue.Enqueue(Weapons[4]);
				
			}
			else
				break;


			system("pause");
			cout << "\n\n";	
		}

	}

	cout << "Receipt\n"
		<< "=======\n";

	float fTotal = 0;

	while(Queue.Dequeue(Weapons[nCartSize]))
	{
		cout << Weapons[nCartSize].GetName() << " - " << Weapons[nCartSize].GetCost() << '\n';
		fTotal += Weapons[nCartSize].GetCost();
	}

	cout << "Total: " << fTotal;

this is my loop where I ask the user to pick an item, but I can't remember on how to remove it from the list of items in the loop.

Recommended Answers

All 8 Replies

Post the declaration of CQueue.

sorry for double post

Post the declaration of CQueue.

This what you wanted? or do you want my queue.cpp

#include <iostream>
using namespace std;
#include "Queue.h"
#include "Weapons.h"
int main( void )
{
	const int nSize = 5;

	CWeapons Weapons[nSize];

	// Set each Weapons w/ unique names 
	Weapons[0].Set( "Batarang", 59.99f, 1);
	Weapons[1].Set( "KeyBlade", 39.99f, 1);
	Weapons[2].Set( "Katana", 39.99f, 1);
	Weapons[3].Set( "Flail", 39.99f, 1);
	Weapons[4].Set( "Assult Rifle", 39.99f, 1);
	

	const int nCartSize = 5;
	
	int nUser;

	CQueue Queue(nCartSize);

	// shopping menu
	for(;;)
	{
		cout << "1) Add an Item to the cart\n"
			<< "2) Check Out\n"
			<< "What would you like to do:";
		if(cin >> nUser && nUser <= 2 && nUser >=1 )
		{
			cin.ignore(INT_MAX, '\n');
			break;
		}
		cin.clear();
		cin.ignore(INT_MAX, '\n');
	}


	// if user selects add items to the cart
	if(1 == nUser)
	{
		int nCheckOut;

		for(;;)
		{

			// show selextion of 5 Weapons
			for(int i = 0; i < nCartSize; ++i)
			{
				cout << i+1 << ") " << Weapons[i].GetName() << " - " << Weapons[i].GetCost() << '\n';
			}
			cout << "0) Check Out Early\n";

			// ask user what option they want to do (0-5)
			for(;;)
			{
				cout << "What weapon would you like to add: ";
				if(cin >> nCheckOut && nCheckOut >=0 && nCheckOut <=5 )
				{
					cin.ignore(INT_MAX, '\n');
					break;
				}
				cin.clear();
				cin.ignore(INT_MAX, '\n');
			}

			if(nCheckOut == 1)
			{
				Queue.Enqueue(Weapons[0]);
		
			}
			else if(nCheckOut == 2)
			{
				Queue.Enqueue(Weapons[1]);	
				
			}
			else if(nCheckOut == 3)
			{
				Queue.Enqueue(Weapons[2]);
				
			}
			else if(nCheckOut == 4)
			{
				Queue.Enqueue(Weapons[3]);
				
			}
			else if(nCheckOut == 5)
			{
				Queue.Enqueue(Weapons[4]);
				
			}
			else
				break;


			system("pause");
			cout << "\n\n";	
		}

	}

	cout << "Receipt\n"
		<< "=======\n";

	float fTotal = 0;

	while(Queue.Dequeue(Weapons[nCartSize]))
	{
		cout << Weapons[nCartSize].GetName() << " - " << Weapons[nCartSize].GetCost() << '\n';
		fTotal += Weapons[nCartSize].GetCost();
	}

	cout << "Total: " << fTotal;

Post Queue.h

As a piece of advice, you could probably make the program a lot more readable if you broke main() down into several separate functions. Also, you could use switch() rather than the long series of if/else if/else statements when testing (for example) nCheckOut - though in that particular case you could instead just use

if (nCheckOut > 0)
                Queue.Enqueue(Weapons[nCheckOut - 1]);

instead.

As a piece of advice, you could probably make the program a lot more readable if you broke main() down into several separate functions. Also, you could use switch() rather than the long series of if/else if/else statements when testing (for example) nCheckOut - though in that particular case you could instead just use

if (nCheckOut > 0)
                Queue.Enqueue(Weapons[nCheckOut - 1]);

instead.

Thanks for the advice, never thought of that for a loop.

Post Queue.h

Here is my Queue.h

#ifndef QUEUE_H_
#define QUEUE_H_
#include "Weapons.h"

typedef CWeapons Item;

class CQueue
{
private:
	struct tNode
	{
		Item data;
		tNode* pNext;
	};

	tNode* m_pHead;
	tNode* m_pTail;

	const int m_nMax;

	int m_nCurrSize;

public:
	CQueue(int nMax = 0);

	~CQueue();

	bool Enqueue(const Item& info);

	bool Dequeue(Item& info);
};
#endif

>>but I can't remember on how to remove it from the list of items in the loop.

I assume you mean how to remove a node from the linked list. All you have to do is iterate through the linked list until you find the node to be deleted. Then adjust the node->next pointer of the node just previous to the one to be deleted so that it pointer to the node after the one to be deleted. Of course since you have a single linked list the code will have to keep track of both the current node and the previous node.

>>but I can't remember on how to remove it from the list of items in the loop.

I assume you mean how to remove a node from the linked list. All you have to do is iterate through the linked list until you find the node to be deleted. Then adjust the node->next pointer of the node just previous to the one to be deleted so that it pointer to the node after the one to be deleted. Of course since you have a single linked list the code will have to keep track of both the current node and the previous node.

Thanks a lot, this helped and fixed my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.