lundi 19 octobre 2020

Writing a fraction class that returns a mixed fraction type

Error list: main.cpp:54:15: error: out-of-line definition of 'gcd' does not match any declaration in 'Fraction' int Fraction::gcd(int denominator, int numerator){ ^~~ main.cpp:59:14: error: use of undeclared identifier 'denenator'; did you mean 'denominator'? return gcd(denenator%numerator, numerator); ^~~~~~~~~ denominator main.cpp:54:23: note: 'denominator' declared here int Fraction::gcd(int denominator, int numerator){ ^ main.cpp:80:1: error: function definition is not allowed here { ^ main.cpp:86:29: error: function definition is not allowed here int Fraction::getFraction() { ^ main.cpp:90:29: error: function definition is not allowed here string Fraction::toString() { ^ main.cpp:95:11: error: function definition is not allowed here int main(){ ^ main.cpp:137:2: error: expected '}' } ^ main.cpp:69:60: note: to match this '{' void Fraction::setFraction(int numerator, int denominator) { ^ 7 errors generated. Not sure if I'm using GCD formula correctly. Program was working before I tried implementing GCD. Print a fraction, its decimal equivalent, and as a mixed fraction. For example, the fraction 7/2 should be 3 1/2.

Here is the code:

#include<iostream>
#include <string>
#include <iomanip>
using namespace std;


class Fraction { //setting fraction class
private:
int denominator;
int numerator;
int gdc(int, int);
void reduce();


public:
Fraction();
Fraction(int, int);
int getNum();
int getDen();

void setNum(int);
void setDen(int);
void setFraction(int, int);
int getFraction();

string toString(); //return as a string
};

int gcd(int den, int num){
if (num == 0){
return den;
}
else{
return gcd(den%num, num);
}
}

//Fraction constructor method

Fraction::Fraction(int numerator, int denominator) {
setFraction(numerator, denominator);
}

void Fraction::setNum(int numerator) {
setFraction(numerator, this->denominator);
}
void Fraction::setDen(int denominator) {
setFraction(this->numerator, denominator);
}

int Fraction::gcd(int denominator, int numerator){
if (numerator == 0){
return denominator;
}
else{
return gcd(denenator%numerator, numerator);
}
}
int Fraction::getNum() {
return numerator;
}
int Fraction::getDen() {
return denominator;
}

void Fraction::setFraction(int numerator, int denominator) {
if (numerator < 0 || denominator < 1) {
this->numerator = 0;
this->denominator = 1;
}
else {
this->numerator = numerator;
this->denominator = denominator;
}

void Fraction::reduce()
{
int GDC = gdc (numerator, demonminator);
numerator /= GCD;
denenator /= GCD;
}

int Fraction::getFraction() {
return numerator / denominator;
}

string Fraction::toString() {
return to_string(numerator) + "/" + to_string(denominator);
}


int main(){

Fraction myfrac;
cout <<"\n The Fraction is: "<< myfrac.toString();
//setting the numerator and denominator
myfrac.setNum(3);
myfrac.setDen(2);
myfrac.getNum();
myfrac.getDen();
cout<<"\n\nFraction is: "<< 
myfrac.getFraction();


cout <<"\n The Fraction is: "<< myfrac.toString();
//setting the numerator and denominator
myfrac.setNum(3);
myfrac.setDen(-2);
cout << myfrac.getNum();
cout << myfrac.getDen();
cout<<"\n\nFraction is: "<< myfrac.getFraction();

cout <<"\n The Fraction is: "<< myfrac.toString();
//setting the numerator and denominator
myfrac.setNum(-9);
myfrac.setDen(4);
cout << myfrac.getNum();
cout << myfrac.getDen();
cout<<"\n\nFraction is: "<< myfrac.toString();
cout << myfrac.getFraction();

cout <<"\n The Fraction is: "<< myfrac.toString();
//setting the numerator and denominator
myfrac.setNum(8);
myfrac.setDen(0);
cout << myfrac.getNum();
cout << myfrac.getDen();
cout<<"\n\nFraction is: "<< myfrac.toString();
cout << myfrac.getFraction();

return 0;


}

Aucun commentaire:

Enregistrer un commentaire