How can I fix this problem?

#include <cstdio>
int add(int a, int b);

int main()
{
int a, b, c;
printf("Enter a numerator: ");
scanf("%d", &a);
printf("Enter a denominator: ");
scanf("%d", &b);
c=add(a,b);
printf("%f",c);
}
int add(int a, int b)
{
return(a/b);
}

How come the result is always in 0.0000 value?
Where can I put system("pause"); in this function?
0 is due to integer division. int/int = int, so normally 3/4 = 0.75 but (since integers round down) 0.75 becomes 0.
you could return a/(float)b; or have a and b float from the beginning.

you'd put system("pause") before returning from main (which you don't do. how can you have an int function that doesn't return an int? ) though you shouldn't use system at all. ( http://www.cplusplus.com/forum/articles/11153/ ). use getchar() or something..
I'm still getting 0 after changing return a/(float)b;

Any more ideas?
add() should return a float or double. :/

Also, since your program is in C++, why not use C++ I/O?

-Albatross
Last edited on
TNT wrote:
I'm still getting 0 after changing return a/(float)b;
add returns int and that has no decimal point. whenever b > a the result is 0.

btw why do you name a function 'add' when it actually performs a division?
Topic archived. No new replies allowed.