Thread: C++ task

  1. #1
    Registered User
    Join Date
    Jan 2023
    Posts
    1

    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;
    }
    Attached Files Attached Files
    Last edited by Salem; 01-07-2023 at 11:47 PM. Reason: Inlined properly formatted code.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Task 7.4, help
    By Franksy in forum C Programming
    Replies: 5
    Last Post: 12-18-2017, 06:01 PM
  2. Help me do my task
    By mykelis in forum C Programming
    Replies: 2
    Last Post: 09-10-2017, 08:38 AM
  3. Need help to do the big task
    By Steve Cao in forum Linux Programming
    Replies: 1
    Last Post: 07-16-2010, 02:01 PM
  4. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  5. Task Bar
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 07-22-2004, 07:37 AM

Tags for this Thread