i've written piece of code. there's class rational numerator , denominator private members. there method tostring() should return rational number string ("numerator/denominator"). unknown reasons me, not return anything. code:
#include <iostream> #include <iomanip> #include <string> #include <sstream> using namespace std; class rational { long int n, d; public: rational(long int n, long int d) { this->n = n; this->d = d; } bool equals(); int compareto(); std::string tostring() { string resn, resd; string str; ostringstream convertn, convertd; convertn << this->n; convertd << this->d; resn = convertn.str(); resd = convertd.str(); str = resn + "/" + resd; return str; } }; int main() { rational rat(2, 3); rat.tostring(); return 0; }
first thought conversion algorithm wrong, , tried returning anything, still nothing. thank in advance.
if want output string, use cout << rat.tostring();
Comments
Post a Comment