Ive finished writing my code (besides the addition and subtraction functions being right) and now I have to calculate the sign to put with the fraction. I was given the proper print function but I need help figuring out how to assign positive or negative to the inputted values and determine if the sign is to be + or -
class fraction
{
private:
int numerator;
int denom;
bool positive;
public:
void inputFrac();
void printFrac();
fraction fracMult(fraction& b);
fraction fracDiv(fraction& b);
fraction fracAdd(fraction& b);
fraction fracSub(fraction& b);
};
void fraction::printFrac()
{
if (!positive)
{
cout << "-";
}
cout << numerator << " / " << denom;
}
void fraction::inputFrac()
{
char tempchar1;
cout<<"Please input the numerator ";
cin>>numerator;
cout<< "Please input the denominator ";
cin>>denom;
cout<<"Is the fraction positive? (Y or N);
cin>> tempchar1; //This is where i think i need to assign a temporary character to Y and then have it return positive
if(tempchar1=='Y')
{
positive;
}
}
fraction fraction::fracMult(fraction& b)
{
fraction temp;
temp.numerator = numerator * b.numerator;
temp.denom = denom * b.denom;
return temp;
}
fraction fraction::fracAdd(fraction& b)
{
fraction temp;
temp.numerator=numerator + b.numerator;
temp.denom=denom + b.denom;
return temp;
}
fraction fraction::fracDiv(fraction& b)
{
fraction temp;
temp.numerator = numerator * b.denom;
temp.denom = denom * b.numerator;
return temp;
}
fraction fraction::fracSub (fraction& b)
{
fraction temp;
temp.numerator = numerator - b.numerator;
temp.denom = denom - b.denom;
return temp;
}
int main(int argc, char** argv) {
fraction f1, f2, fresult;
f1.inputFrac(); //input the first fraction
f2.inputFrac(); //input the second fraction
cout<<endl;
f1.printFrac();
cout<<endl;
f2.printFrac();
cout<<endl;
cout << "The result of a * b is: ";
fresult = f1.fracMult(f2); // calculate a * b
fresult.printFrac(); // print out the result
cout<<endl;
cout << "The result of a + b is: ";// calculate a + b
fresult = f1.fracAdd(f2); // print out the result
fresult.printFrac();
cout<<endl;
cout << "The result of a / b is: ";// calculate a / b
fresult = f1.fracDiv(f2);// print out the result
fresult.printFrac();
cout<<endl;
cout << "The result of a-b is: ";// calculate a - b
fresult = f1.fracSub(f2); // print out the result
fresult.printFrac();
cout<<endl;
return 0;
}
Im trying to give them the option to select by enter Y or N if the fraction is positive Im having trouble passing the result from the input function to the print function
how would that look in my code if you dont mind me asking? or would there be anyway to process the positive in the inputfrac function that can then be printed out by the print function?
Thank you and I would perfer the method of updating the positive variable in the input frac function cuz thats what my assignment calls for if thats possible
Note: I just updated the code to show you what i mean by the positive in the input frac function
updating the positive variable in the input frac function
You can put the same test I have just about anywhere that suits. The advantage with the class/object oriented approach is the fraction object carries the sign with itself explicitly once the object property/attribute is updated just once. :)