digital0011
digital0011

Reputation: 11

The code is not able to take the input for name?

No changes to the class can be done so I cant add any new functions which sets the name for me.

class ele
{

char* name;
public:

ele() :name(nullptr){}
~ele()
{
    if (name)
        delete[]name;
}
char*& GetName(); 
};

#endif

I try to access the name but it gives me error after cin Debug assertion failed. Invalid null pointer.

>   `     char*& ele::GetName()
    {
    cout << "Please Enter the name"<< endl;
        cin >> this->name;
        return  this->name;
      }`

Upvotes: 0

Views: 99

Answers (1)

marcinj
marcinj

Reputation: 50036

If you cannot change your class (and use std::string) you need to at least allocate memory before cin>>this->name, now you are using a null poitner which is UB. So your fix would look as follows:

if (this->name == nullptr)
   this->name = new char[64]; // << !!
cin >> this->name;

Upvotes: 3

Related Questions