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;
}