15,942,154 members
See more:
Create a zoo structure to represent animals at a zoo. Let the structure contain the following data: number (integer), name, weight (in kilograms). Declare an array with three elements of the described zoo structure and test the following functions. Create the following features:
• Function animal_create by which the keyboard is entered: number, name, weight(0.5 t)
• Function output_animal to derive animal data
• Function double_weight that doubles the weight of all animals

What I have tried:

C++
```#include <iostream>;
using namespace std;
int main()
{

struct Zoo
{
int number;
string name;
double weight;
}

Zoo animal_create(int number, string name, double weight)
{
Zoo zoo;
zoo.number = number;
zoo.name = name;
zoo.weight = weight;
retrun zoo;
}

void output_animal(Zoo zoo[3])
{
for (int i = 0; i < 3; i++)
{
cout >> zoo[i];
}
}

Zoo double_weight(Zoo zoo)
{
zoo.weight = zoo.weight * 2;
}

int main()
{
Zoo zoo[3];
int number;
string name;
double weight;
for (int i = 0; i < 3; i++)
{
cin >> number;
cin >> name;
cin >> weight;
animal_create(number, name, weight);
}

output_animal();

cin >> number;
double_weight(zoo[number]);
}
}```
Posted
Updated 2-Jan-23 13:20pm
v2

## Solution 1

For starters, your `animal_create` function returns zoo instance that you ignore - so the data the user entered is lost when the function exits.

To add to that, the instance it returns is local to the function, so it is destroyed once the function ends, so you couldn't save and use it if you wanted to! (This is canned a dangling reference and is a common fault which is very hard to track down)

You don't pass anything to your output animal function at all!

Pass a pointer to the the array element to `animal_create` function so it can fill in the details that the user entered instead; pass the right parameter to your other functions.

CPallini 2-Jan-23 3:32am
Nope, in `C++` you can return a `struct` that way without problems.

## Solution 2

You have a main function embedded within another main function. The compiler is not going to like that. You should remove the outer main function and its braces and the inner main function should work OK for you.

## Solution 3

There are several errors in your code.
I made a working skeleton starting from it. I've also marked some of the errors I recognized.

```#include <iostream> // NO ';' here
using namespace std;

// struct and function declarations OUTSIDE main definition
struct Zoo
{
int number;
string name;
double weight;
};

Zoo animal_create(int number, string name, double weight);
void output_animal(const Zoo & z);
void double_weight(Zoo  z[], size_t items );

constexpr size_t Items = 3;

int main()
{
Zoo zoo[Items];
int number;
string name;
double weight;
for (size_t i = 0; i < Items; i++)
{
cin >> number;
cin >> name;
cin >> weight;
zoo[i] = animal_create(number, name, weight); // assign the i-th item of the array
}

for (size_t i = 0; i < Items; i++)
output_animal(zoo[i]);

double_weight(zoo, Items);

for (size_t i= 0; i < Items; i++)
output_animal(zoo[i]);
}

Zoo animal_create(int number, string name, double weight)
{
Zoo a;
a.number = number;
a.name = name;
a.weight = weight;
return a; // it is 'return' NOT 'retrun'
}

void output_animal(const Zoo & z)
{
}

void double_weight(Zoo zoo[], size_t items )
{
for (size_t i=0; i<items; ++i)
zoo[i].weight = 2 * zoo[i].weight; // double the weight of i-th item
}```

## Solution 4

Although the question was asked rather vaguely, there are a few places here where help would probably be needed.

Here are some points, in order :
1. Comment the outer main out
`// int main()`

2. missing semicolon
`struct Zoo { .. };`

3. typo at return
`return zoo;`

4. wrong direction at cout, missing overload for data type zoo
`cout << zoo[i].name;`

5. use c++ datatype for container
```// Zoo zoo[3];
vector<Zoo> zoo;```

6. Dont forget to add animal to zoo container
`zoo.push_back(animal_create(number, name, weight));`

7. missing parameter at output
`output_animal(zoo);`

8. doubles the weight of ALL animals
```// double_weight(zoo[number]);
double_weight(zoo);```

v2