hi,
can you help with the code to delete a structure object in c++.

Thanks

Recommended Answers

All 4 Replies

Perhaps show how you allocated it to begin with, then we can tell you the best way to get rid of it.

Perhaps show how you allocated it to begin with, then we can tell you the best way to get rid of it.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
int i;
int main()
{
struct
{
int unit_no,order_no;
double unit_price;
char unit_name[16];
}orders[50];
int option,j,k=0,n;
double total_price=0;
char date[9];
start :
 _strdate( date);    //call the date function
cout<<endl<<"\t"<<"\t"<<"   Date  "<<date;
cout<<endl<<endl<<"****Mash Auto Garage Main Menu****"<<endl;
cout<<"1.   Make an Order.   :"<<endl;
cout<<"2.   View Orders In Place.   :"<<endl;
cout<<"3.   Delete an Order.   :"<<endl;
cout<<endl<<endl<<"Enter Your Option i.e 1,2 or 3 : ";
cin>>option;
switch(option)
{
 case 1:i = k;
   do
    {
   cout<<"\nPlease Enter 0 To Go To Main menu,\nAny other Positive digit to proceed : ";
   cin>>n;
    if(n < 0)
    {
    cout<<"\nIncorrect entry";
    goto start;
    }
    else if(n == 0)
    {
    goto exit;
    }
   clrscr();
   cout<<"\nOrder Number : "<<(i+1);
   orders[i].order_no=(i+1);
   cout<<"\nPlease Enter Item Name : ";
   gets(orders[i].unit_name);
   cout<<endl<<"Enter The Part Number  :  ";
   cin>>orders[i].unit_no;
   cout<<endl<<"Enter The Item's Price  :  ";
   cin>>orders[i].unit_price;
   total_price=total_price + orders[i].unit_price;
   i++;
   k++;
   exit:
  }while(n != 0);
   clrscr();
   cout<<"\n Please Press Any Key To Continue :";
   getch();
   clrscr();
   goto start;
 
 case 2:
   clrscr();
   cout<<endl<<"\t"<<"\t"<<"   Date  :  "<<date;                    //display system date
   cout<<endl<<endl<<"Order No."<<"\t"<<" Item Name: "<<"\t"<<" Part No. : "<<"\t"<<" Price: ";
   for(k=0;k<i;k++)
    {
     cout<<endl<<endl<<orders[k].order_no<<"\t"<<"\t"<<orders[k].unit_name<<"\t"<<"\t"<<orders[k].unit_no<<"\t"<<"\t"<<orders[k].unit_price;
    }
   cout<<endl<<endl<<" Total Price = "<<total_price;
   cout<<endl<<endl<<endl<<" Press Any Key To Go Back To Main Menu : ";
   getch();
   clrscr();
   goto start;

 case 3:
   clrscr();
   cout<<endl<<"\t"<<"\t"<<"   Date  :  "<<date;                        //display system date
   cout<<endl<<endl<<"Order No."<<"\t"<<" Item Name: "<<"\t"<<" Part No. : "<<"\t"<<" Price: ";
   for(k=0;k<i;k++)
    {
     cout<<endl<<endl<<orders[k].order_no<<"\t"<<"\t"<<orders[k].unit_name<<"\t"<<"\t"<<orders[k].unit_no<<"\t"<<"\t"<<orders[k].unit_price;
    }
   cout<<endl<<"Enter Order Nunmber To Delete : ";
   
   getch() ;
   goto start;
}
return 0;
}

I would like to be able to select an order and delete it ..

To get to your direct question, there is no good way to delete a element from an array. You could:
1)add a flag to the struct indicating that the current struct can be used or not.
2)or you could overwrite the struct you wish to delete and shift all structs to the right of the deleted struct by one. So, for instance, if there are four structs in the array and you want to delete the second one by overwriting it, then loop through the larray starting at index == 1 as long as the index is less than the last index used (the last index used would be 3 since there are 4 structs in the array before overwriting the desired one) and each time through the loop assign orders[index + 1] to orders[index].

Now for things you didn't ask, but are important anyway:
1) I'd think about a different system of indenting. Yours is very difficult to read and is going to make your life more difficult than it needs to be when your programs become more complicated. Here's an example of one common indentation scheme incorporating at least some of the other ideas I've listed as well:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>

struct Order
{ 
   int unit_no,order_no;
   double unit_price;
   char unit_name[16];
};

int main()
{
   Order orders[50];
   int i, option, j, k=0, n;
   double total_price=0;
   char date[9]; 
   _strdate( date);    //call the date function
   bool more = true;
  
   while(more)
   {
      cout << endl << "\t" << "\t" << "    Date  " << date;
      cout << endl << endl << "****Mash Auto Garage Main Menu****" << endl;
      cout << "1.   Make an Order.   :" << endl;
      cout << "2.   View Orders In Place.   :" << endl;
      cout << "3.   Delete an Order.   :" << endl;
      cout << "4.   Stop this loop:"  << endl;
      cout << endl << endl << "Enter Your Option i.e 1,2, 3 or 4: ";
      cin >> option;

      switch(option)
      { 
         case 1:
            i = k;   
           do    
           {   
              cout<<"\nPlease Enter 0 To Go To Main menu,\nAny other Positive digit to proceed : ";    
              cin>>n;

2) The expectation would be to generally have the variable i declared inside main() and have the struct declared outside main().
3) Learn more about loops so you can ditch the goto statements. They should be rarely used, if ever.
4) Be aware that conio.h is not a standard header and may not be available on all compilers. So if you really want to use it, it will limit the number of users of your program. If you are writing for a specific single end user then restricting your program like this probably isn't a big deal. But if you want to have the biggest group of end users possible, then stay as standard compliant as you can.
5) Staying as standard as possible would also include using the up to date versions of the standard header files and namespaces, if your compiler is "standard compliant".

thanks men!!!

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.