Create an array that holds struct objects C++

2019-09-20 07:55发布

I am a novice programmer and I am creating a program that holds several objects of a struct type. The program needs to accept user input but I don't know how to do it. First, here's the code I'm using to define the struct:

struct Apartment{
        int number;
        string owner;
        string condition;
}ap;

And here's the code I'm using to ask for user input:

cout << "Enter the apartment number: " << endl;
cin >> ap.number;
cout << "Enter the name of the owner: " << endl;
cin >> ap.owner;
cout << "Enter the condition: " << endl;
cin >> ap.condition;

apartment building[50] = { ap.number, ap.owner, ap.condition};

The last line of code is how I am trying to save the object in an array, bu I don't know if it works. I later need to print all the objects, so it would be nice if you helped me with that too. I am using Visual Studio 2013 as a compiler, in case it makes any difference.

3条回答
The star\"
2楼-- · 2019-09-20 08:09

First of all, let's understand what you are doing.

     struct Apartment{
            int number;
            string owner;
            string condition;
    }ap;

Firstly, you create an Apartement struct which have the name "ap".

cout << "Enter the apartment number: " << endl;
cin >> ap.number;
cout << "Enter the name of the owner: " << endl;
cin >> ap.owner;
cout << "Enter the condition: " << endl;
cin >> ap.condition;

Secondly, (I assume that you are in your main function) you are asking the user to enter some information. You have to keep in mind that cin only that the first word and stop at the first whitespace it sees if you plan to save more word, you should use getline. See this link for more information : http://www.cplusplus.com/doc/tutorial/basic_io/

apartment building[50] = { ap.number, ap.owner, ap.condition};

Finally, your last line will crash for two reasons. Firstly, because the type apartment does not exist. I believe you meant to write Apartment. Secondly because you cannot create an array like this. I suggest you to look at this: http://www.cplusplus.com/doc/tutorial/arrays/

I am not sure exactly what you want to do, so I will give you a sample of code that ask a user how many apartment he has and ask the information for the number of apartment he owns.

struct Apartment{
            int number;
            string owner;
            string condition;
    };

int main() {
    cout << "Hello, how many apartment do you own?";
    int nbAppt = 0;
    cin >> nbAppt;
    Apartment appt[nbAppt];
    for(int i = 0; i < nbAppt; i++) {
        cout << "Appartment number " << i << endl;
        cout << "Enter the apartment number: ";
        cin >> appt[i].number;
        cout << "Enter the name of the owner: ";
        cin >> appt[i].owner;
        cout << "Enter the condition: " << endl;
        cin >> appt[i].condition;
    }
}

Ps: Please note that I assumed that you included namespace std; Ps2: I did not include any relevant include.

查看更多
孤傲高冷的网名
3楼-- · 2019-09-20 08:13
struct Apartment{
    int number;
    string owner;
    string condition;
}ap;

void AddApartment(vector<Apartment> &vtnew)
{
    Apartment building;
    cout << "Enter the apartment number: " << endl;
    cin >> building.number;
    cout << "Enter the name of the owner: " << endl;
    cin >> building.owner;
    cout << "Enter the condition: " << endl;
    cin >> building.condition;
    vtnew.push_back(building);
}

void DisplayApt(vector<Apartment> vtnew)
{
    vector<Apartment> ::iterator it;
    for (it = vtnew.begin(); it != vtnew.end(); it++)
    {
        cout << "apartment number" <<it->number;
        cout << "name of the owner" << it->owner;
        cout << "condition" << it->condition;
    }
}

int main()
{
    vector<Apartment> vt;
    AddApartment(vt);
    AddApartment(vt);
    AddApartment(vt);
    DisplayApt(vt);
    return 0;
}

What you are doing in your code is to declare an array for apartment to hold 50 apartments, but when you are using vectors we need not to give the count beforehand, we can keep adding the apartments, without worrying about the size!

查看更多
孤傲高冷的网名
4楼-- · 2019-09-20 08:23

I think you have a confuse here.

Apartment building[50]

means that this is an array of 50 elements and each element is a struct with type Apartment.

For example, it should be like this:

struct Apartment obj1;
struct Apartment obj2;
struct Apartment obj3;

Apartment building[3] = {obj1, obj2, obj3};

And I wonder what is the main purpose of "save object in an array" ?

Every element of an array should have same datatype and every field of a struct may not have same datatype, so that you shouldn't "save object in an array".

查看更多
登录 后发表回答