I am new to OOP programming and I need some help with my homework. I need to input a char* with cin>> but when i initialized the char* as NULL it breakes and gives me the exception thrown "_Str was nullptr", what can I do to fix this problem? I am using a class with protected members, I tried switching to public but it didn't help. As i said i am very new to this so excuse me if I haven't explained properly.
In the code below "Book a" is working correctly.
class Book
{
protected:
char* title;
char* firstName;
char* lastName;
char* company;
int pages;
float price;
}
my parametrized constructor looks like this
Book(const char* title, const char* firstName, const char* lastName, const char* company, int pages, float price)
{
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
this->fistName = new char[strlen(firstName) + 1];
strcpy(this->firstName, firstName);
this->lastName = new char[strlen(lastName) + 1];
strcpy(this->lastName, lastName);
this->company = new char[strlen(company) + 1];
strcpy(this->company, company);
this->pages = pages;
this->price = price;
}
int main()
{
Book a("TESTtitle", "TESTfirstName", "TESTlastName", "TESTcompany", 365, 50);
a.print();
Book c;
char* title=NULL;
char* firstName=NULL;
char* lastName=NULL;
char* company=NULL;
int pages;
float price;
cin >> title;
cin >> firstName;
cin >> lastName;
cin >> company;
cin >> pages;
cin >> price;
}