4 Replies - 978 Views - Last Post: 06 February 2012 - 01:48 PM Rate Topic: -----

#1 mitchell5879   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-February 12

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.

#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);
}




Is This A Good Question/Topic? 0
  • +

Replies To: c++ boolean operator function being called in int main

#2 mitchell5879   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-February 12

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.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

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:
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.
Was This Post Helpful? 1
  • +
  • -

#4 simeesta   User is offline

  • Deadly Ninja


Reputation: 221
  • View blog
  • Posts: 594
  • Joined: 04-August 09

Re: c++ boolean operator function being called in int main

Posted 06 February 2012 - 01:45 PM

This code is ugly:
        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
Was This Post Helpful? 1
  • +
  • -

#5 mitchell5879   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-February 12

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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1