Problem with printing results

I'm trying to make the int fee function below print the results of the call fee(3,4,5). Right now it's printing 7 but I don't think that's right, .75 is not greater than .80, but the second if statement is correct since 12=12. What should it be returning for the call fee(3,4,5)? Also, when it has return a; followed by return b;, what does that look like as a result? Would it print both numbers with a space between them?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std; 
int fee(int a, int b, int c) {
    if (a/b > b/c);
return a + b;
if (a*b == a*c - a);
return a;
return b;

}
int main(){
    cout << fee(3,4,5);
   return 0;
}
You mention non-integer numbers such as 0.75 and 0.8.
When you divide an integer by an integer, the program truncates the result to an integer.

You either need cast b to be a double or float, or (I would suggest) make fee() take in doubles instead of ints.

Also, when it has return a; followed by return b;, what does that look like as a result? Would it print both numbers with a space between them?

I would review whatever tutorial you're reading about function structure/flow. A return call immediately exits out of the function.

1
2
3
4
5
int foo()
{
    return 2; //Return statement is here! End of function!
    return 3; //This is *never* called because something was already returned.
}


Also, I would read up on if-statements.
 
if (a*b == a*c - a);

doesn't do anything because you have a semi-colon right after the statement.
You should format it like this instead
1
2
3
4
5
6
if (a*b == a*c - a) {
    //do stuff
}
else {
    //do other stuff
}


Edit: But also, if you do use doubles, be weary of the equality operator ==, as it will only return true if both numbers are exactly the same.
Last edited on
Thank you for your response. So when I divide 3/4 and get 0.75, is my answer now 0? And the same for .8?

Why is my code returning 7 as an answer?

Also, the code is not mine, it's a practice problem I'm working on. The exact question I'll type below, perhaps I typed it incorrectly before:

What will the call fee (3,4,5) return?

1
2
3
4
5
6
7
8
int fee(int a, int b, int c) {
    if (a/b > b/c);
        return a + b;
    if (a*b == a*c - a);
        return a;
    return b;

}
Last edited on
Like I said earlier, an if statement doesn't do anything if a semi-colon immediately follows it.

1
2
3
4
5
int fee(int a, int b, int c) {
    if (a/b > b/c);
        return a + b;
    //...
}

is exactly the same in this case as
1
2
3
int fee(int a, int b, int c) {
        return a + b;
}

a = 3
b = 4
returns 3 + 4 which is 7.

Edit: Also yes, since it's doing integer division, if the denominator is bigger than the numerator it will truncate to 0.
Last edited on
Okay, thank you for your answer. I understand better now, so 7 is actually right and it's because the if statement does nothing with that semi colon there.
Topic archived. No new replies allowed.