How to get the exact number of times that a function is executed in C++?
c++
Solution
I hope that the following code snippet will solve your issue!
#include<iostream.h>
void callMe()
{
static int count=0;
cout << "I am called " << ++count << " times!\n";
}
int main()
{
callMe();
callMe();
callMe();
callMe();
return 0;
}
Here the static variable will retain its value and it will print the count incremented each time!
Problem
``` #include<iostream> using namespace std; void callMe() { int count=0; cout<<"I am called "<<++count<<" times!\n"; } int main() { callMe(); callMe(); callMe(); callMe(); return 0; } ``` In this case, the output will be ``` I am called 1 times! I am called 1 times! I am called 1 times! I am called 1 times! ``` Instead, I want the output printed as ``` I am called 1 times! I am called 2 times! I am called 3 times! I am called 4 times! ```