what is wrong with the loop? No cout???

Hello!
Please, why is it not writing cout???

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
 int Count(int q){
q=0;
for (int i1=-3; i1<=3; i1++){



for (int i2=2; i2<i1; i2++){

if (i1%i2!=0){
q+=1;
break;}
                            }
                             }



return q;
                }

int main-(){
cout<<Count;
return 0;
}

Error: In function 'int main()':
Line 19: warning: the address of 'int Count(int)', will always evaluate as 'true'

Why 'true', there is no "bool" options???
Many thanks!
Count is the name of a function. When used on it's own, it's a pointer to the function, and will always be positive. It will therefore always evaluate to true.

Did you mean to actually call the Count function and yest the return value? If so, then you need to go back to your textbook and look up the correct syntax for calling functions.

1) Your indentation is atrocious. Proper indentation is critical for writing code which can be easily read by others.

2) There is no reason for 'q' to be a parameter for your count function. Parameters are used for passing input to a function.. .since your function will throw away any value passed in for 'q' (since you assign q to 0 right away), there is not point.

3) You have a stray '-' symbol after main.

4) (the actual cause of your compiler warning) When you call a function, you have to give it parenthesis to indicate you want to call the function. Just specifying the function name gives you a pointer to the function, but does not actually execute any of the function's code.


With those 4 things changed, you have 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
int Count()    // <- you do not need any parameters
{
    int q=0; // <- make q local to this function instead of a parameter
    
    // fix this insane indentation so the code is readable
    for (int i1=-3; i1<=3; i1++)
    {
        for (int i2=2; i2<i1; i2++)
        {
            if (i1%i2!=0)
            {
                q+=1;
                break;
            }
        }
    }

    return q;
}

int main()  // <- get rid of stray '-' symbol
{
    cout<<Count(); // <- add parenthesis after Count to indicate you want to call the function
    return 0;
}
Many many many thanks!

I was right beaking my head thinking how to solve this!!!

Have still 2 questions (which do tno seem easy for me)


1. I thought q should not be a parameter of the function.
But, it seemed to me, parameters should be sth like numbers like i1 and i2...???

Can anyone make me aclear rule or sth, why it should (or might) be empty?


2. I put return 0; instead of break, and the result was 0 instead of 1.

What happened acutally???


MANY THANKS!!!

Topic archived. No new replies allowed.