I need some help, Please!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaRok28
    New Member
    • Feb 2007
    • 8

    I need some help, Please!

    Code:
    #include <iostream>
    using namespace std;
    
    int GCD(int a, int b)
    {
       while(1)
       {
          a = a % b;
             if(a ==0)
                return b;
             b = b % a;
          if(b == 0)
             return a;
       }
    }
    
    int LCM(int x, int y)
    {
       int i;
       if (x > Y)
          for(i = y; i <= x * y; i++)
          {
             if (i % x == 0 && i % y ==0)
                return i;
          }
       else
          for(i = y; i <= x * y; i++)
          {
             if(i % x == 0 && i % y ==0)
                return i;
          }
       return i;
    }
    
    int main()
    {
       char cAgain;
       int x, y;
       
       do
       {
          cout << endl << "Please enter value one: " << endl << endl;
          cin >> x;
          cout << endl << "Please enter value two: " << endl << endl;
          cin >> y;
          cout << endl << "The Greatest Commin Factor of "
               << x << " and " << y << " is " << GCD(x,y) << endl;
          cout << endl << "The Least Common Multiple of " 
               << x << " and " << y << " is " << LCM(x,y) << endl;
          cout << "Would you like to calculate another? Y/y/N/n"
               << endl << endl;
          cin >> cAgain;
       }while(cAgain == 'Y' || cAgain == 'y');
       
       cout << endl << "Thank you for using this program." << endl;
       
       return 0;
       
    }
    There is my code. And it works except when i use the numbers -10 and zero in that order. The error says Floating Point Exception and just stops. So I was wondering what that means and how do I fix it in my code. Thanks
    Last edited by Ganon11; Mar 19 '07, 10:45 PM. Reason: code tags added
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Does this problem occur every time you include a 0, or only when you include a 0 as the second number, or only when you include -10? I suspect the error comes when you try and use modulus division by 0, but I can't be sure.

    Comment

    • DaRok28
      New Member
      • Feb 2007
      • 8

      #3
      Originally posted by Ganon11
      Does this problem occur every time you include a 0, or only when you include a 0 as the second number, or only when you include -10? I suspect the error comes when you try and use modulus division by 0, but I can't be sure.
      It happens everytime the 0 is the second number.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Probably division by zero, then. You should add a message informing the user that he/she cannot enter 0. If you like, you can also add some error trapping that will prevent the user from entering 0.

        Comment

        Working...