I am stuck over these for hours. Plz help.
1.There are 3 files here: fraction.h - class definition , fraction.cpp - member functions & main.cpp- main function. How to link fraction.cpp with fraction.h? Does it need to link fraction.cpp with main.cpp also? How? Actually I tried to link them. I am not sure what I have done.
2.There are 2 errors in Fraction.cpp. It is specified in the code. It said missing ; before string .That's weird. It doesn't have any string. I can't get it. How to fix them?And I am sure every { matches with } there.How come there is unexpected file end?
3. I wrote a function- add(Fraction num) to get the numerator & denominator of (num1+num2) so that it is called in this way:
ans = num1;
ans.add(num2);
Does it have problem?
This is a simplified version of my code:
header:
Code:
class Fraction
{
public:
Fraction(int numer1, int denom1);
Fraction();
void display();
void add(Fraction num);
private:
int denom,numer;
void reduce();
}
Fraction.cpp
Code:
#include "Fraction.h"
#include <iostream>
using namespace std;
Fraction::Fraction(int numer1, int denom1)
{
numer=numer1;
denom=denom1;
}
Fraction::Fraction()
{
numer=0;
denom=0;
}
void Fraction::display()
{
cout<<numer<<"/"<<denom;
}
void Fraction::reduce()
{
int x;
if (numer>denom)
{
for(x=denom;x>1;x--)
{
if ((numer%x==0) && (denom%x==0))
{
numer=numer/x;
denom=denom/x;break;
}//syntax error : missing ';' before 'string'
//fatal error C1004: unexpected end of file found
}
}
if (numer<denom)
{
for(x=numer;x>1;x--)
{
if ((numer%x==0) && (denom%x==0))
{
numer=numer/x;
denom=denom/x;break;
}
}
}
}
void add(Fraction num)
{
numer=numer+num.numer;
denom=denom+num.denom;
reduce();
}
main.cpp
Code:
#include "fraction.h"
#include <iostream>
using namespace std;
void main()
{
int numerator, denominator;
Fraction num1, num2, ans;
cout << "Input a fraction (a/b): ";
cin >> numerator >> denominator;
num1 = Fraction(numerator, denominator);
cout << "Input another fraction (a/b): ";
cin >> numerator >> denominator;
num2 = Fraction(numerator, denominator);
// addition
ans = num1;
ans.add(num2);
num1.display();
cout << " + ";
num2.display();
cout << " = ";
ans.display();
cout << endl;
}