-5

I am making a finite state machine for a coding class. How can I use the return value to change the value of int HP in my main so I will not have any other problems with my code. I just want to make it able to manipulate the value of HP, and use the new value of HP for more functions.

Sorry if the fix to this problem is really simple. I am struggling to understand how functions work in C++ and cannot seem to find a solution any other place online or reading tutorials.

#include <iostream>
#include <time.h>
using namespace std;

int forage(int HP) {
    cout << "The ant is in foraging state."<<endl;
    if (rand() % 100 < 60) {
            cout << "The ant found something!"<<endl;
        if (rand() % 100 < 10) {
            cout << "The ant found poison!" << endl;
            HP -= 1;
        }
        else {
            cout << "The ant found food!" << endl;
            HP += 1;
        }
    }
    int mHP = HP;
    return mHP;
}


 int main() {
        srand(time(NULL));
        int mHP = 0;
        cout << "Welcome to Ant Simulator"<<endl;

        forage(10);
        cout << mHP;
        system("pause");
        return 0;
    }
  • 3
    Something like `mHP = forage(10);` perhaps? – πάντα ῥεῖ Jan 29 '19 at 19:14
  • 2
    Hi, welcome to Stack Overflow. Given that this is a rather basic question, it might be more beneficial for you to grab and follow a [good C++ book](https://stackoverflow.com/q/388242/1782465); that will give you a more solid, integrated understanding than piecemeal questions on SO can. – Angew is no longer proud of SO Jan 29 '19 at 19:32
  • **WARNING**: Using [`rand()` is considered harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Your use of `time(NULL)` as a random number seed means that this will produce identical results if run in the same second, and on many platforms `rand()` is [*barely* random at all](http://dilbert.com/strip/2001-10-25). – tadman Jan 29 '19 at 19:45

3 Answers3

4

You have a couple of choices. One possibility is to pass HP by reference, and have forage modify what was passed in:

void forage(int &HP) {
    cout << "The ant is in foraging state."<<endl;
    if (rand() % 100 < 60) {
            cout << "The ant found something!"<<endl;
        if (rand() % 100 < 10) {
            cout << "The ant found poison!" << endl;
            HP -= 1;
        }
        else {
            cout << "The ant found food!" << endl;
            HP += 1;
        }
    }
}

Another possibility is to just use the result returned from forage:

mHP = forage(10);

If you're going to do this, you can add an annotation so that a recent compiler will tell you about the problem if you accidentally ignore the value it returned:

[[nodiscard]] int forage(int HP) {
// ...

The [[nodiscard]] tells the compiler you want to be sure the value returned from this function isn't discarded like your code in the question did.

As an aside, I'd also prefer that forage be separated into a couple of separate pieces. I'd prefer to have one piece that deal strictly with the UI (displaying the strings about what happened) and another that deals strictly with the logic of the game itself. As a starting point, you might consider passing a stream as a parameter, and having forage display to that stream:

void forage(int &HP, std::ostream &s) {
    s << "The ant is in foraging state.";
    if (rand() % 100 < 60) {
            s << "The ant found something!\n";
        if (rand() % 100 < 10) {
            s << "The ant found poison!\n";
            HP -= 1;
        }
        else {
            s << "The ant found food!\n";
            HP += 1;
        }
    }
}

This can help with things like porting the game to work under a windowing system, if you ever decide to do something like that.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

Change forage(10); to mHP = forage(10);

Your function forage is of return type int. If you want to get this return value into your variable mHP, you need to assign the return value of the function to the variable as described above.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
InfiniteHigh
  • 202
  • 1
  • 6
  • 1
    Please elaborate, give some explanation of what the change does. – Tobias Wilfert Jan 29 '19 at 19:17
  • 4
    @Abyx The person asking the question has obviously little knowledge about C++ just throwing code at him will change nothing about that. If you add context to your answer he may learn from it. – Tobias Wilfert Jan 29 '19 at 19:22
  • 1
    @Abyx "_How about you give an explanation to your comment?_" In addition to that, all information, relevant to an answer, must be present in the answer itself, not in the comments. – Algirdas Preidžius Jan 29 '19 at 19:37
1

Just to add to the previous answers... For a basic understanding how a function works with your defined function as an example:

int forage(int HP){...}

The int prior to the function name defines the return type, so basically what your function is giving back at the end of execution. Then comes the name of your function, in this case forage, followed by the input parameters. In your case there is only one single input parameter which is an integer value int HP. All the code inside of the curly brackets is executed at function call.

Now all functions that do not have the return type void have a return statement somewhere (most of the times at the end) in their code. The actual return value is assigned to a variable like this:

int returnedValue; 
receivedValue = forage(10); 
MariusW
  • 46
  • 3