I have to create a program that does various operations with rational numbers and prints the numbers as a/b and in decimal form.
Hi,
I am getting the following errors when I try to run my program and I have tired to fix them in every possible way I could think of but nothing seems to do the trick. I would be extremely grateful if you could help me because I have been trying to fix these erros for the past 4 hours (no lie). The errors I get are:
I get this error twice and the error refers toRational c(num1, den 1) and rational d (num2, den2) in my driver.cpp file:
In function `int main()':
no matching function for call to `Rational::Rational(int&, int&)'
candidates are: Rational::Rational(const Rational&)
Rational::Rational()
I also get this error:
[Build Error] [driver.o] Error 1
NOTE: The driver.cpp file was already provied to me so I CANNOT change it
DRIVER.CPP FILE:
//
// driver for rational.cpp
//
#include <iostream>
using namespace std;
#include "Rational.h"
int main()
{
// define numerators and denominators for a pair of rational numbers
int num1 = 7;
int den1 = 4;
int num2 = 3;
int den2 = 7;
// create rational numbers
Rational c( num1, den1 );
Rational d( num2, den2 );
// store result of calculation in object x
Rational x;
// print default object
cout << "By default, x is: ";
x.printRational();
cout << endl << endl;
[code] Your code goes here [/code]
If you not want to create the constructor Rational(int, int), just use the default
The attributes are publics, access them directly.
NOTE: The driver.cpp file was already provied to me so I CANNOT change it
I don't understand what you mean by just use the default and the driver.cpp file was given by my teacher to test my code for the other two files so my other two files have to match with driver.cpp in other words driver.cpp is used to test our program.
This is the only constructor declared in Rational.h Rational(); (that's the default constructor). However in Rational.cpp you have implemented only this Rational::Rational(int numerator, int denominator). ( They don't match )
Hi thanks alot. I was able to fix it and my program runs now. All I had to do was change rational in Rational.h to Rational::Rational(int numerator=0, int denominator=1);. It is able to do all the operations (+,-,*,/) properly after I changed my code around a bit. However, there is something wrong with my reduction function and my printRationalAsFloating function as it does not reduce the fraction or print it in decimal form. For the print as floating, I know the problem is that it prints the number as an int instead of a double. For example, it would print 7/4 as 1. Here is the new code that I have for rational.cpp:
#include "Rational.h"
Rational::Rational(int numerator, int denominator) : num(numerator), den(denominator)
{
}
void Rational::reduction()
{
int smallest;
int gcd;
Rational t;
if (num <den)
smallest=num;
else
smallest=den;
for (int i=2; i<smallest;i++)
{
if (num%i==0&&den%i==0)
gcd=i;
if (gcd!=0)
{
t.num=num/gcd;
t.den=den/gcd;
}
}
}
Rational Rational::printRationalAsFloating() //you are telling that you return a Rational
{
cout << num/den; //that's integer division try num/(float) den
//but you are not returning anything
}
Thank you. You have been so helpful. I will relook at my gcd function and the floating function did work except I used a double instead of float which produces the same output.