#include <iostream>
usingnamespace std;
constint MAX_SIZE = 10 ;
enum HealthType { POOR, FAIR, GOOD, EXCELLENT };
struct AnimalType
{
long id;
string name;
string genus;
string species;
string country;
int age;
float weight;
HealthType health;
};
int main()
{
float animalAgeA;
AnimalType bronxZoo[MAX_SIZE];
for ( int i=0; i < MAX_SIZE ; i++)
{
cout << "Enter " << i+1 << " Animal Name, age , HealthType :" << endl ;
cin >> bronxZoo[i].name >> bronxZoo[i].age >> bronxZoo[i].health;
}
for ( int i =0 ; i < 10 ; i++ )
{
int sum = sum + bronxZoo[i].age ;
animalAgeA = sum /10;
}
cout << "The average age for all animals : " << animalAgeA << endl ;
}
when I write cin for bronxZoo[i].health it give me an error !! "invalid operands to binary expression ('istream' (aka 'basic_istream<char>') and 'int')
why ??
how can I can cin bronxZoo[i].health ??
since I need to display the result for the healthtype
You cannot use std::cin for an enum by default. I can think of three options:
1. Don't use an enum; use a std::string or an int here instead.
2. Input to an intermediary int, and use static_cast to turn it into your enum. This is very dodgy, and could well just not work; need to do some error checking.
3. Write your own stream overload:
1 2 3
std::istream& operator>>(std::istream& in, HealthType& ht) {
// do your conversion from int/string to enum here, or whatever.
}
Personally, I'd just recommend option 1. It's by far the simplest, and you didn't really need an enum here, anyway.