-
1 Attachment(s)
C++ task
Hello, I have a task in C++:
My task is: 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: view in file or:
[COLOR=#24292F][FONT=-apple-system]
Code:
#include<iostream>
#include<windows.h>
#include<iomanip>
#include<string.h>
using namespace std;
struct zoo{
int number;
char name [30];
double kg;
char push_back [30];
};
void animal_create (zoo[]){
cout<<"Number: ";
cin>>zoo.number;
cout<<"Name: ";
cin>>zoo.name;
cout<<"Weight: ";
cin>>zoo.kg;
}
void output_animal(zoo[]){
cout<<"Number "<<setw(10)<<"Name "<<setw(10)<<"Weight "<<"\n";
cout<<zoo.number<<" "<<zoo.name<<" "<<zoo.kg<<"\n";
}
void double_weight(zoo[]){
int i;
for(int i=0; i<3; i++){
zoo[i].kg=zoo.kg*2;
}
}
int main(){
cout<<"Add animal: ";
cin>>zoo.push_back;
cout<<"Enter data: ";
int j;
for(int j=0; j<3; j++)
animal_create(zoo[i]);
return 0;
}
-
Your functions needed variable names as well as types.
Tip: Paste the requirements as comments in the code.
Tip: Make sure simple input and output work before trying to write data manipulation code.
Eg.
Code:
#include<iostream>
#include<iomanip>
using namespace std;
struct zoo{
int number;
char name [30];
double kg;
};
// Function animal_create by which the keyboard is entered: number, name, weight(0.5 t)
void animal_create (zoo &creature){
cout<<"Number: ";
cin>>creature.number;
cout<<"Name: ";
cin>>creature.name;
cout<<"Weight: ";
cin>>creature.kg;
}
// Function output_animal to derive animal data
void output_animal(zoo &creature){
cout<<"Number "<<setw(10)<<"Name "<<setw(10)<<"Weight "<<"\n";
cout<<creature.number<<" "<<creature.name<<" "<<creature.kg<<"\n";
}
// Function double_weight that doubles the weight of all animals
int main(){
// Declare an array with three elements of the described zoo structure
zoo myZoo[3];
for(int j=0; j<3; j++)
animal_create(myZoo[j]);
for(int j=0; j<3; j++)
output_animal(myZoo[j]);
return 0;
}