Logical operators in a function

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
long sum ( int m, int n )

{
int o = 0;
int p = m;

  if (p > n)
     (o = 0);
  else (p <= n)
     (o = o + p), p++;

  cout << "The sum of all integers between " << m << "and " << n << "is: " << o << "." << endl;
}


DevC++ tells me that in line 9 "(p <= n) cannot be used as a function". What's the problem? It's a normal logical operator that I've used in small programs before.
Ah... delete the (p <= n). If you're using else without any need for an else if (and you are), it's redundant. No, actually, it gets in the way.

Also, the () on line 8 are redundant, and 10 can changed to:
1
2
o += p; 
p++;


Also, your function should be returning void...
...and where's your loop?

Okay, sorry, I went on a rampage. So sorry. :(

-Albatross
Last edited on
Of course, thanks. Wood for the trees and all that. I've always been absolutely hopeless at programming, but it's my first session at university and I'm trying not to fail. I tend to test things as I go as well, rather than waiting until I've got a complete module. And of course an if-else is stupid as you say, no loop. What about adding a while loop:
1
2
3
4
5
6
7
8
if (p > n)
     (o = 0);
  else
          while (p <= n)
          {
                (o += p);
                p++;
                }


Formatting is a little messy, but it works.
Topic archived. No new replies allowed.