Victoria
Victoria

Reputation: 21

c++ deleting elements in a struct array

The code below takes a users input for 2 properties and stores it. I've been failing to find a way to delete the information on a property if it is removed from the market by prompting the user to enter the property-ID to be removed and also update the property details such as changing the Asking Price by prompting the user to enter the property-ID. My teacher recommended using a structure array which I have managed to do so far.

#include <iostream>                                           // takes users input and prints out output 
#include<conio.h>                                             // declares several useful library functions for performing "console input and output"
#include <stdio.h>                                            // compiler directive which stores standard input output 
using namespace std;                                          // defines the standard namespace 
struct Property                                               // declares the struct as Property 
{
   int ID[3];                                                // declare variable "ID" of data type interger to identify property ID 
   int Asking_Price;                                         // declare variable "Asking_Price" of data type interger to to identity the asking price for the property
   char Type[10];                                            // declare variable "Type" of data type char to identify the property type (detatched, semi-detatched, terraced, flat, commercial)
   char Address[20];                                         // declare variable "Address" of data type char to identify the property address
};
int main()  

{
   Property My_Property[2];                                      // declare the struct, the array and the number of array elements 
   for (int i=0;i<2;i++)                                         // for loop counts the number of properties from 1 to 5
   {
      cout << "\n  Details of property ID " << i + 1 << " are :\n"; // Prints out the property ID 

      cout << "\t Enter the property asking price : ";             // Prompts the user to enter asking price fopr the property  
      cin >> My_Property[i].Asking_Price;                          // Takes users input and stores it in the array 

      cout << "\t Please enter the property type: ";               //Prompts the user to enter the property type
      cin>>My_Property[i].Type;                                    //Takes users input and stores it in the array 

      cout << "\t Please enter the property address : ";           //Prompts the user to enter the property address
      cin >> My_Property[i].Address;                               //Takes users input and stores it in the array 
   }

   for (int j = 0;j<2;j++)                                      // for loop counts the property information for all properties entered
   {
      cout << "\n Information on property number " << j + 1 << " is :\n";  //Prints out the stored property ID
      cout << "\t Asking Price :" << My_Property[j].Asking_Price<<endl;  //Prints out the stored asking price for the property 
      cout << "\t Property Type : " << My_Property[j].Type<<endl;            //Prints out the stored property type 
      cout << "\t Address : " << My_Property[j].Address<<endl;           //Prints out the stored address for the property

   }

   for (int k=0;k<2;k++)    
   {
      cout << "\n Please enter the property ID for the property you would like to delete : \n ";
      cin>> My_Property[k].ID[3]; 

      if(My_Property[k].ID[3]>2)
      {
         cout<<"\n\n This property ID does not exist, press ENTER to try again: \n";
      }
      else
      {
         //trying to figure out the delete code

      }
      _getch(); //read characters from screen`enter code here`

   }
}

Upvotes: 1

Views: 8538

Answers (1)

Miles Budnek
Miles Budnek

Reputation: 30494

You can't actually delete an item from an array, since arrays have a fixed size. The usual way to remove an item from a fixed-size container is to shift all subsequent elements down one space to overwrite the item you don't want.

Property props[10];
int prop_count = 0;
// Fill props somehow, incrementing prop_count each time you add an item
std::cout << "\n Please enter the property ID for the property you would like to delete : \n ";
int to_delete;
std::cin >> to_delete;
for (int i = to_delete + 1; i < prop_count; ++to_delete) {
    props[i - 1] = props[i];
}
prop_count -= 1;

This is more-or-less how std::vector works under the covers.

If you don't care about keeping all of your elements contiguous, you could add some way to mark a Property as deleted, but that tends to lead to more complicated code.

In the end, you should probably just use a re-sizable container like std::vector, at which point you could just call std::vector::erase to erase items from it.

Upvotes: 1

Related Questions