# Thread: Static vs. Automatic Variables

1. ## Static vs. Automatic Variables

Hi,

I'm trying to get the second call to SumNumbers a value other than 0. I think that the second call to SumNumber is 0 just like the first call because I have "static int cnt = 0;". I don't know how to change my program to get the second call to say 1. Here is my code:

Code:
```#include <iostream>
#include <fstream>
using namespace std;
int sum;
void SumNumbers();
ifstream NumData;
void main()
{//block 1
SumNumbers();
cout << "Call 1: the sum of the numbers is " << sum << endl;
SumNumbers();
cout << "Call 2: the sum of the numbers is " << sum << endl;
}
void SumNumbers()
{//block 2
NumData.open("numbers.dat");
sum = 0;
int num;
static int cnt = 0;
NumData >> num;
while (cnt <=4)
{//block 3
sum = sum + num;
cnt++;

}
}```
Thanks!

2. I have absolutely no idea what you want to do. The second time the function runs cnt will be equal to 5, and the loop will never execute. (Maybe you should read up on the static keyword)

We're going to need some more detail on what exactly you are trying to do.

3. Code:
```void how_many_times_has_this_function_been_called(void) {
static int calls = 0;

calls ++;

return calls;
}```

4. Code:
```int sum;
void SumNumbers();
ifstream NumData;

void main()```
1)Don't ever declare a variable outside of a function. Either declare it in main() or another function.
2)Here is the program template for all beginning programmers:
Code:
```#include<iostream>
using namespace std;

int main()
{

return 0;
}```
All of your programs should contain that code. Fill in any other code you need.

5. just FYI, the 'return 0;' at the end of main() is implicit.

6. Originally Posted by miken
just FYI, the 'return 0;' at the end of main() is implicit.
It's still good form to write it lest you try to do the same in any other function