Hello,
In my program I need to get a double value when I use the findValue function. But I end up getting an integer value instead. Please 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
using namespace std;
class Rational
{
public:
Rational(int = 0, int = 0);
void assignnewvalues(int, int);
void print();
double findValue();
private:
int numerator, denominator;
};
Rational::Rational(int num, int den)
{
numerator = num;
denominator = den;
}
void Rational::assignnewvalues(int num, int den)
{
numerator = num;
denominator = den;
}
void Rational::print()
{
cout << "The rational number is: " << numerator << "/" << denominator << endl;
}
double Rational::findValue()
{
double value= numerator / denominator;
return value;
}
int main()
{
Rational num1(22, 7);
num1.print();
cout << "The value is: " << num1.findValue() << endl;
system("pause");
return 0;
}
|
Last edited on
You will need to cast one or both of the variables to a double to force a double answer.
return static_cast<double>(numerator) / static_cast<double>(denominator);
Last edited on
That works! I never knew this. Thanks so much!