k not declared in this scope???

k not declared in this scope???
scope???

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
33
34
35
36
37
  int Prime (int array_[5], int st) {


do
{




int e=1;
for (int i=2; i<st; i++){
if (st%i==0)
{
e=0;
break;
}


}
int m=0;
int k=0;
int array_f[5];
if(e==1){array_f[k]=st;}
k+=1;
st+=1;
m+=st;

}// do
while(k==4);
return m;
}

int main(){
int b=23;
cout<<Prime(b);
return 0;
}
If you format your code with tabs so that you can see the scope that all the variables are in, you'll see that:
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
int Prime (int array_[5], int st) 
{
    do
    { //<---------------------------- Start scope of 'do' body
        int e=1;
        for (int i=2; i<st; i++)
        {
            if (st%i==0)
            {
                e=0;
                break;
            }
        }

        int m=0;
        int k=0; // <-------------- 'k' is declared in 'do' body
        int array_f[5];
        if(e==1) { array_f[k]=st; }

        k+=1;
        st+=1;
        m+=st;

    }// do //<---------------------- End scope of 'do' body
    while(k==4); // but 'k' is used outside of that scope.
    return m;
}


Move the declaration of 'k' to outside of the 'do' body.
In a previous post you had the function Prime() which was pretty good.
http://www.cplusplus.com/forum/beginner/117004/#msg638368
Well, it gave the wrong result for Prime(0) and Prime(1), but other than that, it seemed to work.

I recommend that you add any extra code that you require in main() and leave the Prime function alone. Think of it as a tool which does just one job, like a screwdriver or a hammer. Function Prime() does a job, it gives a yes / no answer to tell whether a particular number is prime.

You can then call that function with different values, as required. For example this program (based upon your own code) will print all the prime numbers between 1 and 100:
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> 

using namespace std;

int Prime (int st) 
{
    if (st<2)
        return 0;
        
    int e = 1;
    for (int i=2; i<st; i++)
    {
        if (st%i==0)
        {
            e = 0;
            break;
        }
    }

    return e;
}

int main()
{
    for (int b=0; b<100; b++)
    {
        if (Prime(b))
            cout << b << " ";
    }
    cout << endl;
    return 0;
}


As for the question about k out of scope, the variable k is declared within a block enclosed by braces, like this:
1
2
3
4
{
    int k = 5;
}
cout << k; // error 

When the closing brace is reached, the variable k no longer exists, it cannot be referred to or used at all. The solution is to declare it outside of the block:
1
2
3
4
5
int k = 0;
{
    k = 5;
}
cout << k; // ok 
Hello!
All clear, but have unfortunately to follow rules of the task, because it is so said to do!

We have to do it to proove we understand that wey teachers want.:((...so that I do not have too much freedom...:(
MANY THANKS ANYWAY!!!
Last edited on
Hello!
There is no condition in if (Prime(b)) ???

what is it supposed to mean?
sorry, sheer beginner

MANYY THANKS!
There is no condition in if (Prime(b)) ???

Yes there is. First the function Prime(b) is called. The returned value is an integer, either 0 to mean "not prime" or 1 meaning "yes, it is prime).

so if (Prime(b)) after the function call will give either if (0) or if (1)

When any integer is tested as if it was a boolean value like this, a value of zero means false, any other number means true.

That is what I thought, as it was int instead of bool I ws not sure.

But what is happening after that?
We are actually chaning b form 0 to 100 first, then, in the next line , we have just 2 solutions (0 and 1) ???

Many thanks!

There is a for-loop, which executes for each value of b from 0 to 99. Inside the body of the loop, the function Prime() is called with the current value of b. If the number is prime, it is then printed.
Hello, Chervil!
So, when b=0, Prime() is not called, when b=1 we call Prime(), and then,up to 100, we do not call Prime().!!


Well, then we could easier have:

for (int b=0; b<1; b++)
{
if (Prime(b))
cout << b << " ";
}

or at least sth like:

for (int b=0; b<10; b++)
{
if (Prime(b))
cout << b << " "; --> that way we simply save time,
principl is the same!!!
}

Hope I got it now!!!
Many thanks!

Last edited on
enemy wrote:
So, when b=0, Prime() is not called, when b=1 we call Prime(), and then,up to 100, we do not call Prime().!!

I'm not sure what you mean here.
In the example I posted previously, it read like this:
1
2
3
4
5
    for (int b=0; b<100; b++)
    {
        if (Prime(b))
            cout << b << " ";
    }

What happens in the for loop...
1. at line 1, b is declared. b is given the initial value of 0
2. the condition (b<100) is tested
3. it is true, so the body of the loop is executed, so yes, we do call Prime(0), etc.
4. after reaching the closing brace at line 5, execution goes back to the start of the loop. b is increased by 1, so b is now 1.
5. the condition (b<100) is tested.
6. ... and so on.

Look at it this way, this is what the body of the loop actually does
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
        if (Prime(0))
            cout << 0 << " ";
        if (Prime(1))
            cout << 1 << " ";
        if (Prime(2))
            cout << 2 << " ";
        if (Prime(3))
            cout << 3 << " ";
        if (Prime(4))
            cout << 4 << " ";
...
...
...
        if (Prime(97))
            cout << 97 << " ";
        if (Prime(98))
            cout << 98 << " ";
        if (Prime(99))
            cout << 99 << " ";

Topic archived. No new replies allowed.