Passing variables

Hi all, i'm a beginner starting this program for exam study purposes but i've come to a halt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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.

jsmith, thank you...

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.
Last edited on
So your ageAddOne() function now looks like this: ?

1
2
3
void ageAddOne( animal_info& b ) {
    b.age = b.age + 1;
}


Right?

So the way I read it:

1
2
3
void changeHealth( animal_info& b ) {
// ...
}

Sorry, i didn't clarify, b.healthy is a bool...

no worries tho... b.healthy = !b.healthy works just fine.

Thanks.

edit: although, the question asks me to change a specific animal's health... how do you specify an animal and change the status using only 1 command?
Last edited on
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.

Topic archived. No new replies allowed.