This one of those typical Rational number programs. It takes a user-supplied fraction and then another. Then it does the 4 basic math operations on it. Finally it compares the fractions to each other. I have the program working fine. I just want to know how to fix this warning message. I don't quite know "what to return" from this block of code. TIA!
Wow, after all that I forgot to add the warning message:
warning: control reaches end of non-void function
//allows cin >> f; (f is a fraction)
istream &operator>>(istream &input, Rational &f)
{
string stringy;
//stringify the input stream
input >> stringy;
//find where the "/" is at in the string
int marker = stringy.find_first_of("/");
//numerator is from start of number to "/"
string top = stringy.substr(0, marker);
//denominator is from "/" to end
string bottom = stringy.substr(marker + 1, stringy.size() - marker);
//convert strings to ints
istringstream up(top);
int topper;
up >> topper;
istringstream low(bottom);
int lower;
low >> lower;
//set fraction
f.numer = topper;
f.denom = lower;
}
Here is all the code, for reference. It is separated into 3 files. Header, implementation, and driver.
//Rational.h
#ifndef RATIONAL_H_
#define RATIONAL_H_
#include <iostream>
using std::istream;
using std::ostream;
class Rational
{
friend istream &operator>>(istream &, Rational&);
friend ostream &operator<<(ostream &, const Rational&);
public:
Rational(int = 0, int = 1);
Rational operator+(const Rational &);
Rational operator-(const Rational &);
Rational operator*(const Rational &);
Rational operator/(const Rational &);
bool operator==(const Rational &) const;
bool operator!=(const Rational &) const;
bool operator>(const Rational &) const;
bool operator<(const Rational &) const;
bool operator>=(const Rational &) const;
bool operator<=(const Rational &) const;
private:
int numer;
int denom;
void simplify();
};
#endif /*RATIONAL_H_*/
//Rational.cpp
#include <iostream>
using std::cout;
using std::istream;
using std::ostream;
#include <string>
using std::string;
#include <sstream>
using std::istringstream;
#include "Rational.h"
//No Denominators <= 0
Rational::Rational(int n, int d)
{
if (d==0)
{
cout << "Illegal Denominator of 0, or negative Denominator\n";
cout << "Setting Denominator to default 1\n";
d = 1;
}
else if (d<0)
{
cout << "Denominator is Negative...\n";
cout << "Reversing Sign of Numerator and Denominator\n";
n = -n;
d = -d;
}
numer = n;
denom = d;
}
//allows cin >> f; (f is a fraction)
istream &operator>>(istream &input, Rational &f)
{
string stringy;
//stringify the input stream
input >> stringy;
//find where the "/" is at in the string
int marker = stringy.find_first_of("/");
//numerator is from start of number to "/"
string top = stringy.substr(0, marker);
//denominator is from "/" to end
string bottom = stringy.substr(marker + 1, stringy.size() - marker);
//convert strings to ints
istringstream up(top);
int topper;
up >> topper;
istringstream low(bottom);
int lower;
low >> lower;
//set fraction
f.numer = topper;
f.denom = lower;
}
//allows cout << f; (f is a fraction)
ostream &operator<<(ostream &output, const Rational &f)
{
return output << f.numer << "/" << f.denom;
}
//Add 2 Rationals
Rational Rational::operator+(const Rational &add)
{
Rational temp;
temp.numer = add.numer * denom;
temp.numer += add.denom * numer;
temp.denom = add.denom * denom;
temp.simplify();
return temp;
}
//Subtract 2 Rationals
Rational Rational::operator-(const Rational &sub)
{
Rational temp;
temp.numer = sub.denom * numer;
temp.numer -= denom * sub.numer;
temp.denom = sub.denom * denom;
temp.simplify();
return temp;
}
//Multiply 2 Rationals
Rational Rational::operator*(const Rational &mult)
{
Rational temp;
temp.numer = mult.numer * numer;
temp.denom = mult.denom * denom;
temp.simplify();
return temp;
}
//Divide 2 Rationals
Rational Rational::operator/(const Rational &div)
{
Rational temp;
temp.numer = div.denom * numer;
temp.denom = denom * div.numer;
temp.simplify();
return temp;
}
//Test Rational == Rational
bool Rational::operator==(const Rational &eq) const
{
return (numer == eq.numer && denom == eq.denom);
}
//Test Rational != Rational
bool Rational::operator!=(const Rational &dne) const
{
return (numer != dne.numer && denom != dne.denom);
}
//Test Rational > Rational
bool Rational::operator>(const Rational >) const
{
Rational temp;
temp.numer = numer * gt.denom;
temp.denom = denom * gt.numer;
return (temp.numer > temp.denom);
}
//Test Rational < Rational
bool Rational::operator<(const Rational <) const
{
Rational temp;
temp.numer = numer * lt.denom;
temp.denom = denom * lt.numer;
return (temp.numer < temp.denom);
}
//Test Rational >= Rational
bool Rational::operator>=(const Rational >e) const
{
Rational temp;
temp.numer = numer * gte.denom;
temp.denom = denom * gte.numer;
return (temp.numer >= temp.denom);
}
//Test Rational <= Rational
bool Rational::operator<=(const Rational <e) const
{
Rational temp;
temp.numer = numer * lte.denom;
temp.denom = denom * lte.numer;
return (temp.numer <= temp.denom);
}
//simplify Rational
void Rational::simplify()
{
int largest;
largest = numer > denom ? numer : denom;
int gcd = 0;
for (int loop = 2; loop<=largest; loop++)
if (numer % loop == 0 && denom % loop == 0)
gcd = loop;
if (gcd != 0)
{
numer /= gcd;
denom /= gcd;
}
}
//driver.cpp
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <iomanip>
using std::ios_base;
#include "Rational.h"
int main()
{
Rational a, b;
cout << "Enter the first fraction:" << endl;
cin >> a;
cout << "Enter the next fraction:" << endl;
cin >> b;
cout << "Here are your fractions:" << endl;
cout << a << endl;
cout << b << endl;
cout << "Okay, let's do some math:" << endl;
cout << a << " + " << b << " = " << a+b << endl;
cout << a << " - " << b << " = " << a-b << endl;
cout << a << " * " << b << " = " << a*b << endl;
cout << a << " / " << b << " = " << a/b << endl;
//boolalpha used to display true or false
cout << "Okay, let's compare them:" << endl;
cout << a << " == " << b << " ? ";
cout.setf(ios_base::boolalpha);
cout << (a==b) << endl;
cout << a << " != " << b << " ? ";
cout.setf(ios_base::boolalpha);
cout << (a!=b) << endl;
cout << a << " > " << b << " ? ";
cout.setf(ios_base::boolalpha);
cout << (a>b) << endl;
cout << a << " < " << b << " ? ";
cout.setf(ios_base::boolalpha);
cout << (a<b) << endl;
cout << a << " >= " << b << " ? ";
cout.setf(ios_base::boolalpha);
cout << (a>=b) << endl;
cout << a << " <= " << b << " ? ";
cout.setf(ios_base::boolalpha);
cout << (a<=b) << endl;
return 0;
}