return not working properly?
Nov 2, 2012 at 2:51am Nov 2, 2012 at 2:51am UTC
Hi, I am having trouble with returning a parameter from one of my functions. Can someone help?
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
#include <iostream>
using namespace std;
int quotient(int numerator,int denomenator,int answer)
{
answer = (numerator/denomenator);
return answer;
}
int main()
{
int numerator = 1;
int denomenator = 1;
int answer = 1;
cout << "Enter numerator: " ;
cin >> numerator;
cout << "Enter denomenator: " ;
cin >> denomenator;
quotient(numerator,denomenator,answer);
cout << answer;
}
When I run the program, I get an answer of 1 no matter what, even though I am returning answer from my function. Help?!
Last edited on Nov 2, 2012 at 2:52am Nov 2, 2012 at 2:52am UTC
Nov 2, 2012 at 3:04am Nov 2, 2012 at 3:04am UTC
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
#include <iostream>
using namespace std;
int quotient(int numerator,int denomenator)
{
int answer;
answer = (numerator/denomenator);
return answer;
}
int main()
{
int numerator = 1;
int denomenator = 1;
int answer;
cout << "Enter numerator: " ;
cin >> numerator;
cout << "Enter denomenator: " ;
cin >> denomenator;
answer = quotient(numerator,denomenator);
cout << answer;
}
This is the correct way to return values. Although your quotient will *always* be an integer, because you specified it as such. (-1, 0, 1, 2, [...]) If you want to have it return a decimal value (1.5, 1.0251, ...) use type "double" instead of type "int" for all your values.
Nov 2, 2012 at 3:12am Nov 2, 2012 at 3:12am UTC
Yup, I want it to truncate, thanks though it works now!
Topic archived. No new replies allowed.