The is a digit figure 1 printed in every line

The output is not exactly as it should be.
Here is the correct output:
1
2
3
4
5
Count to what value? 4
Starting count, level   1: val == 4
Starting count, level   2: Val == 3
Starting count, level   3: val == 2
Starting count. level   4: val == 1

The Displaying values part prints okay.
My output is this:
1
2
3
4
Starting count, level   1: val == 414
Starting count, level.  2: val == 313
Starting count, level.  3: val == 212
Starting count, level.  4: val == 111

Not exactly as it should be.
[/code]
Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  
#include<iostream>
using namespace std;

//Program to count recursively
//int level;
void count(int val)
{   int level = 0;
    //Write some information about the level os recursion
cout<<"Starting count: val == "<<val << ++level;cout <<" val == "<< val<<endl;
//Actual recursion occurs if the condition is  true
    if(val > 1)
      count (val - 1);
//Main task line for function count
 
    cout<<"\t\t\t\t**** Displaying val: " << val <<endl;
    cout <<"Leaving count, level val == \n" << level --;
}

int main()
{
    int level = 0;
    void count(int);
    int how_high;
    cout <<"Count to what value? ";
    cin >> how_high;
    level = 0;
    count (how_high);
The output you show:
Starting count, level 1: val == 414

does not match the code:
cout<<"Starting count: val == "<<val << ++level;cout <<" val == "<< val<<endl;

You say that "Starting count, level" is being output, but nowhere in your code is the "Starting count, level" being output.

So are you actually showing us the actual code?
My output is this:
No. Your output is:
Count to what value? 4
Starting count: val == 41 val == 4
Starting count: val == 31 val == 3
Starting count: val == 21 val == 2
Starting count: val == 11 val == 1
				**** Displaying val: 1
Leaving count, level val == 
1				**** Displaying val: 2
Leaving count, level val == 
1				**** Displaying val: 3
Leaving count, level val == 
1				**** Displaying val: 4
Leaving count, level val == 
1


The reason for the second value is (line 10) << ++level. The third: cout <<" val == "<< val<<endl;. Just remove << ++level;cout <<" val == "<< val and you get what you want.

What is level good for anyway? If you want the level of recursion you need to pass level as a second [reference] parameter (within the function you shall not set it to 0):

void count(int val, int& level)

Pointer would be also ok.
Last edited on
Here is one way to do it with a static level counter:
1
2
3
4
5
6
7
8
9
10
11
12
void count(int val)
{
    static int level = 0;       // only gets initialized once
    ++level;

    cout<<"Starting count, level.  "<< level
        << ": Val == "<< val<<endl;
    if(val > 1)
      count (val - 1);

    --level;
}

The disadvantage of this is that it isn't thread-safe.

Another option with default parameters. Notice that the call from main() doesn't specify a level, so it gets the default (1). The recursive calls give the level
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

//Program to count recursively
//int level;
void count(int val, int level = 1)
{
    cout<<"Starting count, level.  "<< level
	<< ": Val == "<< val<<endl;
    if(val > 1)
	count (val - 1, level+1);
}

int main()
{
    int how_high;
    cout <<"Count to what value? ";
    cin >> how_high;
        count (how_high);
}


This disadvantage of this method is that the top level caller could be a jerk and pass a non-zero starting level.
Thanks to all. I finally got it right and got the correct output.
Topic archived. No new replies allowed.