Abid Hasan
Abid Hasan

Reputation: 13

How can I store sum of array in this condition

problem is in inner for loop

for (int i=0, j=0; i<n; i++, j++){
        if (i != j){
            cout << sum = sum + arr[j] <<endl;
            *//error: overloaded function type*
        }
    }

cout << sum = sum + arr[j] <<endl;

Upvotes: 0

Views: 120

Answers (1)

paddy
paddy

Reputation: 63471

This is a problem with operator precedence.

It can be resolved by putting the expression in parentheses:

cout << (sum = sum + arr[j]) << endl;

However, this is pretty horrible style, and you should split it into two lines instead:

sum += arr[j];
cout << sum << endl;

Your future self, and anyone else who reads your code, will thank you.

Upvotes: 4

Related Questions