-1

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;
}
Bdawg
  • 3
  • 2
  • 1
    You need to allocate memory where `cin` will write to. It doesn't do that for you – UnholySheep Jan 04 '19 at 12:30
  • 4
    If you used `std::string` instead of `char*`, your problem would solve itself. If you insist on using `char*`, you have to **allocate memory** for the string first and only then assign something to it. – Yksisarvinen Jan 04 '19 at 12:30
  • 2
    You need a tutorial or instruction on the difference between an object and a pointer to an object. – Raedwald Jan 04 '19 at 12:31
  • I would have used string instead of char* if I knew what else I would need to change, I edited and added my parametrized constructor. I used char* because that is the only example the teacher taught us. – Bdawg Jan 04 '19 at 12:43

2 Answers2

1

As this question is tagged C++, you should think twice about pointers. High level C++ program should avoid rawe pointers: references, smart pointers and standard containers should be enough.

If you want to look behind the scenes, you must see a pointer as an address: you cannot do anything with it unless it points to a valid object. Here you have initialized your pointers to NULL so you cannot dereference them. You must assign them with valid characters array before using them

#define SIZE 32
char cTitle[SIZE];
char *title = cTitle;

But remember: this is rather C-ish code, and you should not use it unless you have to interface will C code or are building a custom low level object.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

You use std::string in much the same way you use int (but you have to #include it's definition, it isn't a keyword). It's also generally better to accept any std::ostream to output to, rather than always std::cout. Note that using namespace std is a bad idea.

#include <string>
#include <iostream>

struct Book
{
    std::string title;
    std::string firstName;
    std::string lastName;
    std::string company;
    int pages;
    float price;
}

// more idiomatic that "Book::print"
std::ostream& operator << (std::ostream& os, const Book & b)
{
    os << b.title;
    os << b.firstName;
    os << b.lastName;
    os << b.company;
    os << b.pages;
    os << b.price;
    return os;
}

std::istream& operator >> (std::istream& is, Book & b)
{
    is >> b.title;
    is >> b.firstName;
    is >> b.lastName;
    is >> b.company;
    is >> b.pages;
    is >> b.price;
    return is;
}

int main()
{
    Book a { "TESTtitle", "TESTfirstName", "TESTlastName", "TESTcompany", 365, 50 };
    std::cout << a;

    Book c;  
    std::cin >> c;

    std::cout << c;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Thank you, also if I am going to use your implementation, my destructor would remain blank ? – Bdawg Jan 04 '19 at 15:12