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?
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
}
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.