Adding fractions in C ++

So basically I'm trying to make a fraction class. It will take a fraction from user input and perform the addition. For example, I print 1 5 and 1 7, for adding it will print 12/35.
Here is my .h class:

#include <string> #ifndef _FRACTION_H_ #define _FRACTION_H_ using namespace std; class Fraction { public: Fraction(); Fraction(int n, int d); int getNumerator() const; int getDenominator() const; void display(); string to_string(); Fraction operator+(Fraction &second); private: int numerator; int denominator; }; 

And this is my .cpp file:

 #include "Fraction.h" #include <string> include <iostream> using namespace std; Fraction::Fraction(){} Fraction::Fraction(int n, int d) { this->numerator = n; this->denominator = d; } int Fraction::getNumerator() const { return numerator; } int Fraction::getDenominator() const { return denominator; } Fraction Fraction::operator+(Fraction &second) { int n1 = getNumerator() * second.getDenominator(); int n2 = second.getNumerator() * getDenominator(); int d = getDenominator() * second.getDenominator(); return Fraction(n1+n2, d); } string Fraction::to_string() { return (getNumerator() + "/" + getDenominator()) ; } 

And this is my main method:

 bool get_input(Fraction &fract); int main() { Fraction fraction1, fraction2; if (((!get_input(fraction1)) || (!get_input(fraction2)))) cout << "Invalid Input!" << endl; else { // Test harness for Arithmetic Operator Overloading Fraction result = fraction1 + fraction2; cout << "Addition = " << result.to_string() << endl; } bool get_input(Fraction& fract) { int num, den; cout << "Enter numerator & denominator (separated by space)" << endl; cin >> num >> den; if (cin.fail()) return false; Fraction f(num,den); fract = f; return true; } } 

He managed to enter user input. But, however, it does not print the result. Thanks in advance.

+4
source share
2 answers

There may be other problems, but the Fraction::to_string() function is clearly incorrect: the types in the expression return int , char const* and int . The result of adding these is char const* , but provided that the string literal has only two characters, if the sum of two int greater than two, you have undefined behavior. You need to convert int for strings first; The easiest way to do this is to use std::ostringstream :

 std::string Fraction::to_string() { std::ostringstream results; results << numerator << '/' << denominator; return results.str(); } 

You can usually expect compiler errors when used improperly with types like this, but for historical reasons: string literals are of type char const[] , not std::string ; char const[] converted almost everywhere to char const* ; C ++ support adding integer values ​​to pointers; and std :: string has a constructor that does an implicit conversion from char const* .

+6
source

You use the "+" operator between literals ( "/" ) and int in your to_string method to_string The compiler tries to perform an implicit conversion and ends with point (int + int + char *, it is best to assume that it is char *), which then converts to std :: string using the correct constructor

Change your method to use stringstream (and you will have finer formatting control):

 string Fraction::to_string() { std::stringstream s; s << getNumerator() << "/" << getDenominator(); return s.str(); } 

link: http://www.cplusplus.com/reference/sstream/stringstream/

+1
source

Source: https://habr.com/ru/post/1491073/


All Articles