#include<iostream>
#include<cstring>
using namespace std;
class city
{
      protected:
                char *name;
                int len;
                
      public:
             void getname(void)
             {
                  char *s;
                  s= new char[30];
                  cout<<"Enter city name: ";
                  cin>>s;
                  len= strlen(s);
                  name= new char[len+1];
                  strcpy(name,s);
                  }
             void printname()
             {
                  cout<<name<<"\n";
                  }
};
int main()
{
    city *cptr[10];
    int n=1;
    int option;
    do
    {
        cptr[n]= new city;
        cptr[n]->getname();
        n++;
        cout<<"Do you want to enterone more name?\n";
        cout<<"(Enter 1 for yes 0 for no):";
        cin>>option;
        }
        while(option);
    cout<<"\n";
    for(int i=1; i<=n; i++)
    {
            cptr[i]->printname();
}
delete []cptr;
system("pause");
return 0;
}

i am getting an error after the program is over my compiler shuts down........can anyone tell whats the matter???

here is what I came up with:

the problems I fixed:

  • you were trying to delete the cptr array itsself. not the things inside it, so I added a loop to do this
  • array values start at 0 so I changed some loops or you would get segmentation faults trying to access the item above the one you wanted (eg 2 items you would have instead of 0 and 1, printed item 1 and 2 (and 2 would not exist!!))
  • maybe not necessarily, but I added memsets to make sure the string terminates (if its filled with rubbish it may think its longer than it actually is)
  • removed system("pause") in favour of cin.get() (does the same thing but cin.get() is less resource heavy ect..)

this now compiles ok with minGW and runs fine for me. see if it works for you.

#include<iostream>
#include<cstring>

using namespace std;


class city
{
      protected:

		char *name;
		int len;

      public:
			void getname(void)
			{
				char *s;
				s= new char[30];
				memset(s, '\0', sizeof(char) * 30);
				cout<<"Enter city name: ";
				cin>>s;
				len= strlen(s);
				name= new char[len+1];
				strcpy(name,s);
			}
			void printname()
			{
				cout<<name<<"\n";
			}
};
int main()
{
    city *cptr[10];
    int n = 0;
    int option;
    do
    {
        cptr[n]= new city;
        cptr[n]->getname();
        n++;
        cout<<"Do you want to enter one more name?\n";
        cout<<"(Enter 1 for yes 0 for no):";
        cin>>option;
	}
	while(option);

    cout<<"\n";

    for(int i=0; i<n; i++)
    {
		cptr[i]->printname();
	}

	for(unsigned short i = 0; i < n; ++i)
	{
		delete cptr[i];
	}

        
	cout<< "\npress enter to exit:";
	cin.ignore(100, '\n');
	cin.get();

	return 0;
}
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.