Returning a sum, while not counting repeating values?

I am trying to add 3 values and return the sum, not counting any repeating values. I am trying to get it to look like this for example, it's hard to explain:
loneSum(1, 2, 3) → Returns 6
loneSum(3, 2, 3) → Returns 2
loneSum(3, 3, 3) → Return 0


Here's what I have so far, i managed it print the 6, but I can't get the rest to work. It compiles and runs and just prints the 6. Anyone know why?


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
  #include <iostream>
using namespace std;

int loneSum(int a, int b, int c); // Function prototype

int main ()
{
    bool returnVal1 = loneSum(1, 2, 3);
    bool returnVal2 = loneSum(3, 2, 3);
    bool returnVal3 = loneSum(3, 3, 3);


    return 0;
}

int loneSum(int a, int b, int c) // Function definition

{
    if (a == b)
        return c;

    if (a == c)
        return b;

    if (b == c)
        return a;

    else
        cout << a + b + c << endl;
}
Last edited on
Hi,

==>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int loneSum(int a, int b, int c); 
int main ()
{
    int returnVal1 = loneSum(1, 2, 3);
    int returnVal2 = loneSum(3, 2, 3);
    int returnVal3 = loneSum(3, 3, 3);

    cout << returnVal1 << endl;
    cout << returnVal2 << endl;
    cout << returnVal3 << endl;
    return 0;
}

int loneSum(int a, int b, int c)
{
    int sum = 0;
    if (a != b && a != c) sum += a;
    if (b != a && b != c) sum += b;
    if (c != a && c != b) sum += c;
     return sum;
}
Does that help you? :)
Here's what I have so far, i managed it print the 6, but I can't get the rest to work. It compiles and runs and just prints the 6. Anyone know why?

Because you seem to be confuzzled about what loneSum is doing. Is it printing the value or returning it?

If it's returning it, print the returned value in the calling code. If it's printing it, make sure you print for all possible values, not just the case where none of the variables are equal.
@closed account 5a8Ym39o6 yes, that did it!
Glad it helped :)
Topic archived. No new replies allowed.