Jump to content

C++ struct removing element

ahuckphin
Go to solution Solved by Mira Yurizaki,

I'd argue don't have a stack where you can delete from any position, because it's no longer a stack by definition. If you need to delete from any position, then use another data access paradigm and structure.

I'm almost done with my assignment. However apart from data validation, I have been unable to get "delete any item records" to work. 

 

I'm using Dev C++ and my array is stored as a stack. I'm consered that because a stack is a stack, you can only delete from the top. However my friend said that this is an array and the concept of deleting an element from an array applies to stack as well. 

So now I'm panacking concerend that I would have to rewrite the entire thing with linked list. 

 

#include <iostream>
#include <string>

using namespace std;

struct furniture
{
	string material, status;
	char name[30];
	double value;
}product[99];

int main()
{
	clrscr();
	
	int arraySize;
	
	cout << "Welcome to EasyBuy. To get started, please input details of your furnitures." << endl;
	cout << "You will be first asked the number of furnitures you wish to add followed by the name, material, value and status for each furniture." << endl;
	cout << "Please enter the number of furnitures you wish to add: ";
	cin >> arraySize;
	

	for (int i=0; i< arraySize; i++){
		cout<< "Furniture #" << i << endl;
		cout << "Enter furniture name (up to 30 characters): ";
		cin >> product[i].name;
		cout << "Enter type of material (W - wood, P - Plastic, G- Glass, M - Marble, O - others): ";
		cin >> product[i].material;
		cout << "Enter the value of the item (in MYR): ";
		cin >> product[i].value;
		cout << "Enter the status of the product (new or second hand): ";
		cin >> product[i].status;
		}
	}

	int userSelection; 
	int secondArraySelection;
	bool selection = false;
	bool selectionTwo = false;
	
	do {
		cout << "What would you like to do next?" << endl;
		cout << "Press 1 to ammend."  << endl;
		cout << "Press 2 to delete."  << endl;
		cout << "Press 3 to list all records."  << endl;
		cout << "Press 4 to exit." << endl;
		cin >> userSelection;
	
		switch(userSelection){
			case 1:
				cout << "You have selected 1." << endl;
				cout << "Please type in the array position of the furniture you would like to amend the record of: ";
				cin >> secondArraySelection;
				cout << "This furniture current details is as follow:" << endl;
				cout << "Name: " << product[secondArraySelection].name << endl;
				cout << "Material: " << product[secondArraySelection].material << endl;
				cout << "Value: " << product[secondArraySelection].value << endl;
				cout << "Status: " << product[secondArraySelection].status << endl;
				
				int caseOneUserSelection;
				
				do {
					cout << "What would you like to amend of this furniture?" << endl;
					cout << "Press 5 to amend name." << endl;
					cout << "Press 6 to amend material." << endl; 
					cout << "Press 7 to amend value." << endl;
					cout << "Press 8 to amend status." << endl;
					cout << "Press 9 to cancel amending." << endl;
					cin >> caseOneUserSelection;
					
					switch(caseOneUserSelection){
						case 5:
							cout << "What would you like to change the name too? "<< endl;
							cin >> product[secondArraySelection].name;
							cout << "The name has been successfully updated to: " << product[secondArraySelection].name << endl;
							break;
						case 6:
							cout << "What would you like to change the material too? (W - wood, P - Plastic, G- Glass, M - Marble, O - others) " << endl;
							cin >> product[secondArraySelection].material;
							cout << "The material has been successfully updated too: " << product[secondArraySelection].name << endl;
							break;
						case 7: 
							cout << "What would you like to change the value too? " << endl;
							cin >> product[secondArraySelection].value;
							cout << "The value has been successfully updated too: " << product[secondArraySelection].name << endl;
							break;
						case 8:
							cout << "What would you like to change the status too? " << endl;
							cin >> product[secondArraySelection].status;
							cout << "The status has been successfully updated too: " << product[secondArraySelection].status << endl;
							break;
						default:
							cout << "Invalid Input. Please try again. "<< endl;
							break;
							break;
						case 9:
							cout << "Ammending operation cancelled" << endl;
							selectionTwo = true;
							break;
					}
				}
				
				while (!selectionTwo);
				break;
				
			case 2:
				cout << "You have selected 2" << endl;
				
				
				break;
			case 3:
				cout << "You have selected 3" << endl;
				cout << "This is the complete list of furnitures you have inputted" << endl;
				cout << "ID			Name			Material		Value			Status" << endl;
				cout << "--------------------------------------------------------------------------------------------------------------" << endl;
				for (int i=0; i < arraySize; i++ )
				{
					cout << i << "\t\t\t" << product[i].name << "\t\t\t" << product[i].material << "\t\t\t" << product[i].value << "\t\t\t" << product[i].status << endl; 
				}
				break;
			default:
				cout << "Invalid Input. Please try again." << endl;
				break;
				break;
			case 4:
				cout << "Thank you for using EasyBuy. Have a great day";
				selection = true;
				break;
		}
	
	}		
		
	while (!selection);

}

 

It starts first with asking for user input for data. Then it gives the user choice for what action to perform. List all records and amending records wasn't that difficult to acomplish. But, "delete any item records" has. "delete any item records" basically means to delete a record at a time. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×