Consecutive function call problem

I've defined one function and am taking 2 values off it by calling it 2 times. Like this:

count1=counter(subject1);
couunt2=counter(subject2);

But the problem is that the process is being run only once.

But if I modify the code like this:

count1=counter(subject1);
cout << count1 << endl;
count2=counter(subject2);

Then the process is ok. This is also true for cout << ""; in the place of the previous cout.

I can't seem to understand what is really going on.

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
29
#include <iostream.h>
int digits(long long);
int main()
{
    long long num1, num2;

    while(cin >> num1 >> num2)
    {
        int lnm1, lnm2;

        lnm2=digits(num2);
        cout << "";
        lnm1=digits(num1);
    }
    return 0;
}

int digits(long long n)
{
    int i, j, rem;

    for (i=10, j=1 ; rem>=0 ; i*=10, j++)
    {
        rem=n-i;
        cout << "rem for " << n << "=" << rem << endl;
    }

    return j-1;
}


[b]EDIT::::::::[/B]
Later I did something this
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
29
30
31
32
#include <iostream.h>
int digits(long long);
int main()
{
    long long num1, num2;

    while(cin >> num1 >> num2)
    {

	int lnm1;
	lnm1=digits(num1);
	//cout << lnm1 << endl;

	int lnm2;
	lnm2=digits(num2);
	//cout << lnm2 << endl;
    }
    return 0;
}

int digits(long long n)
{
    int i, j, rem;

    for (i=10, j=1 ; rem>=0 ; i*=10, j++)
    {
	rem=n-i;
	cout << "rem for " << n << "=" << rem << endl;
    }

    return j-1;
}


This is now working correctly but my question is why can't it be like the first one?
Last edited on
In function digits, rem, which is used to control the loop, is uninitialised.
Thanks "kbw". Your opinion was right. But just to make things clear for myself, why does work the way I said it did. I mean:

int lnm1;
{function call}
int lnm2;
{function call}

Why does it work this way? When the rem is uninitialized?
cout << count1 << endl; is a series of function calls that use the stack. They could leave some value in the memory location that rem eventually occupies, making it take a different value between calls.

It's most likely zero the first time and non-zero the second time.
Topic archived. No new replies allowed.