static int does not work! Help?

i always remains 0. Any idea why? (This is an over simplification of my code, so as to not waste everybody's time.)

1
2
3
4
5
6
7
8
9
void solve();
{
static int i = 0;

i = i + 1
if ( i==10) {return;}
solve();
}
Last edited on
This won't even compile, so maybe you "over-simplified" a bit much.

break doesn't make any sense here. Presumably you meant return.
http://msdn.microsoft.com/en-us/library/vstudio/s1sb61xd.aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;
void showstat( int curr ) {
   static int nStatic;    // Value of nStatic is retained
                          // between each function call
   nStatic += curr;
   cout << "nStatic is " << nStatic << endl;
}

int main() {
   for ( int i = 0; i < 5; i++ )
      showstat( i );
}


nStatic is 0
nStatic is 1
nStatic is 3
nStatic is 6
nStatic is 10

I am making the assumption this is the way you are trying to use static?
Last edited on
Thanks! And yes I am trying to use static.
Last edited on
Sorry
With corrections done so it compiles:

http://ideone.com/9xAVXv

Since solve cannot return until i is 10, i clearly does change.
Topic archived. No new replies allowed.