A
A
Archpriest Metapop2020-10-19 03:19:44
C++ / C#
Archpriest Metapop, 2020-10-19 03:19:44

I can't deal with structures in C++. How to solve the problem?

According to the condition, you need to create a structure consisting of the name, gender and height of a person, and then calculate the average height of the girls.
But it's not entirely clear to me how to organize the memory and where the data will be stored?
+ I do not quite understand how to enable the addition of a new element.

const int n = 50;
struct info {
    string Name[n];
    int H;
    char Sex[n];
};

Well, the filling function of this structure looks like this:

info WriteStruct(info) {
    info man;
    cin.get();
    cout << "Введите Имя: ";
    cin << man.Name();
    cout << "Введите Рост: ";
    cin << man.Н;
    cout << "Введите пол: [M]/[F]";
    cin << man.Sex();
    return man;
}

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WinPooh32, 2020-10-19
@caramel14

stringName[n];

Why do you need an array of strings? std::string manages arrays internally.
Since you have c++, then use only std::string (char arrays are usually needed to interact with C libraries).
struct info {
    std::string Name;
    int H;
    char Sex;
};

Take std::vector as the storage structure .
An example of working with it in your case:
std::vector<info> people;
info man = WriteStruct(info);
people.push_back(man);

You can iterate through the array like this:
for (int i = 0; i < people.size(); i++) {
   info man = people[i];
}

upd: WriteStruct should be renamed to ReadStruct because this name better describes the process taking place in it.

D
Developer, 2020-10-19
@samodum

You need to create an array info[n]
And remove the arrays inside the structure

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question