But i can't put the bool in that function other wise it will always reset to false.
Another way: declare your boolean variable in main(), and then send it to the function as an argument. If you make the function parameter a reference, which just means you put a '&' symbol to the right of the type, then the function will be able to change the value of the variable back in main(). Here is an example:
Code:
#include <iostream>
using namespace std;
void myFunc(bool& aFlag, int num)
{
if(aFlag)
{
cout<<num<<endl;
}
else
{
cout<<"Flag is false."<<endl;
aFlag = true;
}
}
int main()
{
bool myFlag = false;
myFunc(myFlag, 10);
myFunc(myFlag, 5);
return 0;
}
The parameter variable aFlag becomes a synonym, or alternate name, for myFlag back in main(), and as a result myFlag and aFlag are the same thing.