#include <iostream>
#include "animal_info.h"
using std::cout;
using std::cin;
using std::endl;
using std::list;
using std::string;
using std::istream;
istream& read(istream& is, animal_info& s)
{
is >> s.type >> s.age >> s.healthy;
return is;
}
void displayAnimals(const animal_info& a)
{
cout << endl;
cout << "Animal Details " << endl;
cout << "Type: " << a.type << " with Age: " << a.age << endl;
cout << "Health_status: " << a.healthy << endl;
cout << endl;
}
/* this is where i'm getting stuck...
it's obviously supposed to add 1 to the age of the animal...
the value of c is b.age + 1 but i don't know the syntax for passing it back to int main()
*/
int ageAddOne(const animal_info& b)
{
cout << "ageAddOne test: " << b.age << endl;
int c = b.age + 1;
cout << "ageAddOne test again: " << c << endl;
return c;
}
int main()
{
list<animal_info> animalInfo;
animal_info record;
cout << "Enter a type, age of the animal and health status: " << endl;
while (read(cin, record))
{
animalInfo.push_back(record);
}
for (list<animal_info>::const_iterator iter = animalInfo.begin();
iter != animalInfo.end(); ++iter)
{
cout << "testing:" << iter->age << endl;
ageAddOne(*iter);
cout << "testing again:" << iter->age << endl;
displayAnimals(*iter);
}
system("PAUSE");
return EXIT_SUCCESS;
}
Read half way down the code to find where i'm getting stuck
Please can anyone help me pls... would b appreciated.
b.age is a variable just as int x is a variable. How do you increment x? b.age is incremented in the same way.
ageAddOne does not need to return a value; rather, it should modify the object passed into it. Thus ageAddOne should be "void ageAddOne( /* ... */ )". Your main program ignores the return code anyway.
BUT, you are passing the animal by CONST reference to ageAddOne, which means that ageAddOne is NOT allowed to modify the animal. Adding 1 to one of its member variables is considered modifying the object.
Your reply helped me loads... helped me fix it by removing the const and replacing c with b.age = b.age + 1. Woot :)
edit:
I've just read now that i have to add a function that will change a specific animal's health status and that this function's body must only consist of 1 command.
Sigh, i wouldn't even know where to start... so if someone could please guide me... i would be grateful.
The way I read your problem statement above, I interpreted it to mean that once you've find a "specific animal", that you have 1 line of code to change it. ie, I think you already solved the problem.