c++ code not working, my output screen crashes

My output screen crashes whenever I try to execute my code. This is the part of the question:

Declare a class named House for a real estate locator service. The following information should be included:

  • Owner: (a string of up to 20 characters)
  • Address: (a string of up to 20 characters)
  • Bedrooms: (an integer)
  • Price (floating point)

b) Declare available to be an array of 100 objects of class House.

c) Write a function to read values into the members of an object of House.

d) Write a driver program to test the data structures and the functions you have developed.

The driver program should read in house entries into the available array. After the code for entering the data, you should write code to output the data that you have entered to verify that it is correct.

Here’s my code:

class House {
private:
    string owner;
    string address;
    int bedrooms;
    float price;

public:
    House(string owner = "", string address = "", int bedrooms = 0, float price = 0.0)
    {
        this->owner = owner;
        this->address = address;
        this->bedrooms = bedrooms;
        this->price = price;
    }
    void setOwner(string owner)
    {
        this->owner = owner;
    }
    void setAddress(string address)
    {
        this->address = address;
    }
    void setBedrooms(int bedrooms)
    {
        this->bedrooms = bedrooms;
    }
    void setPrice(float price)
    {
        this->price = price;
    }
    string getOwner()
    {
        return owner;
    }
    string getAddress()
    {
        return address;
    }
    int getBedrooms()
    {
        return bedrooms;
    }
    float getPrice()
    {
        return price;
    }
    void getData()
    {
        cout << "Enter Owner : ";
        getline(cin, owner);
        setOwner(owner);
        cout << "Enter Address : ";
        cin >> address;
        setAddress(address);
        cout << "Number of Bedrooms? : ";
        cin >> bedrooms;
        setBedrooms(bedrooms);
        cout << "Price : ";
        cin >> price;
        setPrice(price);
        cout << endl;
    }
    void display()
    {
        cout << "Owner t Address t Bedrooms t Price" << endl;
        cout << getOwner() << "t" << getAddress() << "t" << getBedrooms() << "t" << getPrice() << "t" << endl;
    }
};
int main()
{

    House* h[100];
    int time = 0;
    char yesorno;

    do {

        h[time]->getData();
        h[time]->display();

        cout << "Do you wish to continue ?";
        cin >> yesorno;
        time++;
    } while (yesorno == 'y' || yesorno == 'Y');
    return 0;
}

Answer

The most obvious problem is that you do the following:

House* h[100];
    /* ... */
        h[time]->getData();

The definition of h creates an array of 100 pointers to House; it does not create a single house! With h[time]-> you try to access a house supposedly pointed to by h[time] but that house does not exist.

A “C solution” would be to simply declare h as an array of houses and access them via the . operator:

House h[100];
    /* ... */
        h[time].getData();

In C++ one would typically use a vector and add houses via push_back().


Additionally, there is one minor hiccup: After entering a string or a number on its own line, which is the typical way (you enter the number and press enter), the newline at the end of that line is still in the pending input; a successive getline() will see that immediate newline and read an empty line.

This happens after each loop in your program because the input ends with entering a number; at the beginning of the next house input, however, is a getline() because a name can have more than one word. Therefore the program does not wait for any user input but assigns an empty string as the name.

The easy fix is to loop until the line is not empty. (As a side effect, that catches the user hitting enter more than once):

do
{
    getline(cin, owner);
}while(owner == "");

You probably want to read an entire line for the address as well, with the same logic.

Otherwise your program is working fine!