Question

I need to create the following struct in C++ id (integer) - identifies the element texture...

I need to create the following struct in C++

  1. id (integer) - identifies the element
  2. texture (string) - specifies the appearance of the element (rabbit, fox, etc.)
  3. color (integer)
  4. health (double)
  5. Dimensions – length, width, and height. All three values are doubles (these variables will be also stored in struct "dimension" )
  6. Hostile (Boolean) – determines if the animal is hostile (1) or peaceful (0)

I need a function (outside the struct) that will read the data and inport from the following txt file into an array. The first line is a number indicating the number of elements in the file.

10
0    rabbit        brown        10    .3    .1    .1    0
1    rabbit        brown         10    .4    .1    .1    0
2    rabbit        grey         10   .2    .1    .1    0
3    deer         brown         25   2    .3    .7    0
4    deer         brown         25   1.5 3   .9   0
5    fox       orange       16   1.1   .2   .4   0
6    fox       orange        16   .9   .3   .3   0
7    rat           black       9   .2   .1   .05   1
8    rat            black       6   .1   .08   .05   1
9    pigeon        grey       1   .03   .01   .02   0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <fstream>

#define MAX 10

using namespace std;


//dimension structure with l b and h values

struct dimension

{

    double l;

    double b;

    double h;

};

//pets structures to store data from input file

struct pets

{

    int id;

    string texture;

    string color;

    double health;

    struct dimension Dimensions;

    bool hostile;

} petDetails[MAX]; //array of MAX elements here MAX is 10 you can increase it

//for dynamic Dimensions vectors can be used


//function to read input from given file name it stores data into the array of structures

//and return number of elements stored in array

int readFromFile(string fileName)

{

    ifstream file(fileName); //opening file

    //if not opend then exit

    if (!file.is_open())

    {

        cout << "Input File is not present!" << endl;

        exit(1);

    }

    //take inputs from file

    int numberOfPets;

    //first input is an integer denoting number of elements

    file >> numberOfPets;

    //taking input for each line and storing them into appropriate variable

    for (int i = 0; i < numberOfPets; i++)

    {

        file >> petDetails[i].id;

        file >> petDetails[i].texture;

        file >> petDetails[i].color;

        file >> petDetails[i].health;

        file >> petDetails[i].Dimensions.l;

        file >> petDetails[i].Dimensions.b;

        file >> petDetails[i].Dimensions.h;

        file >> petDetails[i].hostile;

    }

    // returning number of elements stored in the array

    return numberOfPets;

}

//diplay all attribute of a pet structutre

void displayPet(struct pets pet) {

    cout<<"Id: "<< pet.id<<endl;

    cout<<"Texture: "<< pet.texture<<endl;

    cout<<"Color: "<< pet.color<<endl;

    cout<<"Health: "<< pet.health<<endl;

    cout<<"Length: "<< pet.Dimensions.l<<" ";

    cout<<"Breath: "<< pet.Dimensions.b<<" " ;

    cout<<"Height: "<< pet.Dimensions.h<<endl;

    cout<<"Hostile: "<< pet.hostile<<endl;

    cout<<"======================================"<<endl;

}




//main function driver

int main(int argc, char const *argv[])

{

    string fileName;

    //ask for file name input

    cout << "Enter a input file name: ";

    cin >> fileName;

    //get number of elemnts in the array

    int length = readFromFile(fileName);

    //print all the elements

    for (int i = 0; i < length; i++)

    {

        displayPet(petDetails[i]);

    }

    return 0;

}

Output:

/*If you like the answer please give a like and for queries ask in comment :)*/

Add a comment
Know the answer?
Add Answer to:
I need to create the following struct in C++ id (integer) - identifies the element texture...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i need help with C and D of the phylogenetic tree diagram. this is what i...

    i need help with C and D of the phylogenetic tree diagram. this is what i have for A and B. thank you!! Choose 2 of the 3 documents below. For each document: a. Draw a Table with all 6 species and the 6 or 7 features that you choose to classify, b.Draw a matrix in which you write the number of differences between each one, c. Draw a phylogenetic tree based on these numbers of differences, and d. Label...

  • (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file...

    (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...

  • This should be in PHP, please For Project 1, you need to create a Web form...

    This should be in PHP, please For Project 1, you need to create a Web form to present a ten (5) Multiple Choice quiz about the Chinese zodiac signs. Indicate that all of the questions must be answered for the quiz to be graded. You can use radio buttons or drop-down boxes Multiple Choice answers. Requirements for Project 1: Provide fields for visitors to enter their name and answers to the questions Notify the visitor that the fields are required...

  • Hi! I need help with a C++ program In this assignment, you will create some routines...

    Hi! I need help with a C++ program In this assignment, you will create some routines that will later be used in a "Black Jack" program. Your output on this first part will look something like: card's name is QC with a black jack value of 10 Unshuffled Deck AC/1 2C/2 3C/3 4C/4 5C/5 6C/6 7C/7 8C/8 9C/9 TC/10 JC/10 QC/10 KC/10 AD/1 2D/2 3D/3 4D/4 5D/5 6D/6 7D/7 8D/8 9D/9 TD/10 JD/10 QD/10 KD/10 AH/1 2H/2 3H/3 4H/4 5H/5...

  • int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t;...

    int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t; while (c) { Band3 = 0; printf(" Enter Resistance Value: "); scanf_s("%d", &a); printf("\n Enter Tolerance Value: "); scanf_s("%f", &t); b = t * 100; while (a % 10 == 0) { Band3++; a = a / 10; } Band2 = a % 10; a = a / 10; Band1 = a % 10; printf("\n Band1 = %d, Band2 = %d, Band3 = %d...

  • The following code must be written and run with no errors in java eclipse Description of...

    The following code must be written and run with no errors in java eclipse Description of the code to be written: A resistor is an electronic part colour-coded with three coloured bands to indicate its resistance value. This program returns the resistance of a Resistor object instantiated with three Strings, representing the three colour bands of a real resistor. Start by creating an abstract class 'AbstractResistor' that implements the Comparable interface. This class should have the following three private methods:...

  • Figure 1(a) shows a combination of series and parallel circuit while Table 1 lists the colour...

    Figure 1(a) shows a combination of series and parallel circuit while Table 1 lists the colour code of 4-band resistor. The voltage source, vs is indicated in Figure 1(b). (a) Find the nominal value of resistor Rand the range of its actual resistance. (2 marks) (b) Using the nominal value of RL, compute the effective voltage across Rt. (6 marks) (c) Using the nominal value of Ru, calculate the average power dissipated in RL. (2 marks) 350 22 ş 150...

  • Create 4 arrays: a 1-D array to store the students’ id, 1-D array to store the...

    Create 4 arrays: a 1-D array to store the students’ id, 1-D array to store the students’ names, a parallel 2-D array to store the test scores, and a parallel 1-D array to store letter grades. Read data into the 3 arrays from the file data121.txt. If the file is not found, the program needs to terminate with this error, “Data file not found.”            Calculate and store a letter grade into a 1-D array based on the total score,...

  • Please help. Part 3: Using the color coding for resistors, determine the resistance and the tolerance...

    Please help. Part 3: Using the color coding for resistors, determine the resistance and the tolerance for the following 3 resistors: exornn Rbolor coding: brown, black, yellow, gold Rgcolor coding: red, violet, orange, gold Rocolor coding: orange, orange, orange, gold The chage nowng thonuh a sece is every themes Find out the outest throsgh the sistance is diven e well se the colot code fur esch of the resistors 4-Band-Code 2%, 5%, 10% 560k Ω 5% 1ST BAND | 2ND...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT