#include <iostream>
using namespace std;
int num;
int denom;
int num2;
int denom2;
int num3;
int denom3;
int num4;
int denom4;
bool more;
int frac1;
int frac2;
void ReadFraction()
{
do{
cout << "Enter a valid numerator for the first fraction: ";
cin >> num;
}
while (num > 256 || num < 0);
do{
cout << "Enter a valid denominator for the first fraction, the value must be greater than 0: ";
cin >> denom;
}
while (denom > 256 || denom <= 0);
cout << endl;
do{
cout << "Enter a valid numerator for the second fraction: ";
cin >> num2;
}
while(num2 > 256 || num2 < 0);
do{
cout << "Enter a valid denominator for the second fraction, the value must be greater than 0: ";
cin >> denom2;
}
while(denom2 > 256 || denom2 <=0);
cout << endl;
do {
cout << "Enter a valid numerator for the third fraction: ";
cin >> num3;
}while (num3 > 256 || num3 <0);
do {
cout << "Enter a valid denominator for the third fraction, the value must be greater than 0: ";
cin >> denom3;
}while(denom3 >256 || denom3 <=0);
cout << endl;
do{
cout << "Enter a valid numerator for the fourth fraction: ";
cin >> num4;
}while(num4>256 || num4 <0);
do{
cout << "Enter a valid denominator for the fourth fraction, the value must be greater than 0: ";
cin >> denom4;
}while(denom4 > 256 || denom4 <=0);
cout << endl;
}
bool isgreater (int &num, int &num2, int &denom, int &denom2)
{
double frac1 = num/denom;
double frac2 = num2/denom2;
if (frac1 > frac2)
{
more = true;
}
else {more = false;}
return more;
}
int main()
{
ReadFraction();
isgreater(num, num2, denom, denom2);
{
if(more = true)
cout << num << "/" << denom << " is greater than " << num2 << "/" << denom2 << "\n";
else
{cout << num << "/" << denom << " is not greater than " << num2 << "/" << denom2 << "\n\n";}
}
isgreater(num2, num3, denom2, denom3);
{
if(more = true)
cout << num2 << "/" << denom2 << " is greater than " << num3 << "/" << denom3 << "\n";
else
{cout << num2 << "/" << denom2 << " is not greater than " << num3 << "/" << denom3 << "\n\n";}
}
isgreater(num2, num4, denom2, denom4);
{
if(more = true)
cout << num2 << "/" << denom2 << " is greater than " << num4 << "/" << denom4 << "\n";
else
{cout << num2 << "/" << denom2 << " is not greater than " << num4 << "/" << denom4 << "\n\n";}
}
isgreater(num3, num4, denom3, denom4);
{
if(more = true)
cout << num3 << "/" << denom3 << " is greater than " << num4 << "/" << denom4 << "\n";
else
{cout << num3 << "/" << denom3 << " is not greater than " << num4 << "/" << denom4 << "\n\n";}
}
return(0);
}
4 Replies - 978 Views - Last Post: 06 February 2012 - 01:48 PM
#1
c++ boolean operator function being called in int main
Posted 06 February 2012 - 01:26 PM
Hello, I am having trouble getting the isgreater function to work properly when trying to use it in the maim function. I believe the mistake must be in the boolean function. Any thoughts or comments are appreciated.
Replies To: c++ boolean operator function being called in int main
#2
Re: c++ boolean operator function being called in int main
Posted 06 February 2012 - 01:34 PM
When the code is run it shows that the first fraction is always greater than the second one, even when this is not true.
#3
Re: c++ boolean operator function being called in int main
Posted 06 February 2012 - 01:38 PM
There is a difference between assignment and comparison. Assignment uses just one equals sign and comparison uses two. Example:
Also, when you divide two integers, the result is an integer even if it's being stored in a float or double.
int i = 10; // assignment if(i == 10) // comparison
Also, when you divide two integers, the result is an integer even if it's being stored in a float or double.
#4
Re: c++ boolean operator function being called in int main
Posted 06 February 2012 - 01:45 PM
This code is ugly:
This does the same thing and is much nicer:
I'd convert the fractions to have the same common denominator then compare the integer numerators.
e.g.
is 3/4 > 4/5? 3/4 = 15/20, 4/5 = 16/20. Now it becomes obvious.
These may be useful to you:
Lowest Common Multiple
Greates Common Divisor
if (frac1 > frac2)
{
more = true;
}
else {more = false;}
return more;
This does the same thing and is much nicer:
return frac1 > frac2;
I'd convert the fractions to have the same common denominator then compare the integer numerators.
e.g.
is 3/4 > 4/5? 3/4 = 15/20, 4/5 = 16/20. Now it becomes obvious.
These may be useful to you:
Lowest Common Multiple
Greates Common Divisor
#5
Re: c++ boolean operator function being called in int main
Posted 06 February 2012 - 01:48 PM
Ah of course, can't believe I kept overlooking such a mistake. Thanks you!
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|