Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

Create an array that holds struct objects C++

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...