#include <iostream>
#include <windows.h>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <string.h>

double numerator, denominator;
static short multiplycounter;
static short dividecounter;
const double ten = 10;
double* ppnumerator = &numerator;
double* ppdenominator = &denominator;
using namespace std;

/**********************************************************************************/

double lcm(double numerator, double denominator)
{
 if(*ppnumerator > *ppdenominator)
 {
  for(long counter = long(*ppdenominator) / 2; counter > 1; counter--)
  {
   if(long(*ppdenominator) % counter == 0 && long(*ppnumerator) % counter == 0)
    { *ppdenominator /= counter, *ppnumerator /= counter; }}}
    
 //********************************************************************
 
  if(*ppnumerator < *ppdenominator)
  {
   for(long counter = long(*ppnumerator) / 2; counter > 1; counter--)
  {
   if(long(*ppdenominator) % counter == 0 && long(*ppnumerator) % counter == 0)
    { *ppdenominator /= counter, *ppnumerator /= counter; }}}
}

bool isprime(long denominator)
{
 long max = long(sqrt(denominator));
 if(denominator <= 1)
  { return true; }
 if(denominator % 2 == 0)
  { return false; }
 for( long counter = 2; counter <= max; counter++)
  if(denominator % counter == 0)
   { return false; }
}

bool isprime(double x)
{
 long max = long(x / 2); /* Cast it to remove warnings*/
 if (long(x) <= 0 || long(x) == 1 || (long(x) % 2 == 0 && long(x) != 2))
  { return false; }
 for (long counter = 3; counter < max; counter++)
  if (long(x) % counter == 0)
   { return false; }
  return true;
}



int main()
{
  static double decimal, ndecimal;
  cin >> decimal;
  ndecimal = floor(decimal);
  
  if(decimal != ndecimal)
  {
   for(multiplycounter = 0; decimal != floor(decimal); multiplycounter++)
    {
     if(decimal != floor(decimal))
     decimal *= 10;
    }//loop
   cout << "\nNew non-decimal number: " << decimal;
   cout << "\nNumber of times multiplied by ten: " << multiplycounter << "\n\n";
  }//if statement
  else
  { cout << "That number isn't a decimal!"; }
  
  numerator = long(decimal);
  denominator = pow(ten, static_cast<int>(multiplycounter));
  cout << numerator << " / " << denominator << "\n\n";
  
  if(isprime(numerator) || isprime(denominator))
  { cout << "Cannot be reduced!\n"; }
  while(!isprime(denominator))
  { lcm(numerator, denominator);
    if(isprime(denominator) || isprime(numerator))
     { cout << *ppnumerator << " / " << *ppdenominator; break;}
  }
   
 
 /**********************************************************************************/
 
 cin.clear();
 cin.ignore(255, '.');
 cin.get();
 return 0;
}

Most of this program works. Depending on the decimal number you feed it, it will work properly. But some numbers (515.55) won't. Why? I found that with that number, (and others) the program gets stuck looping the isprime** function. Again, why?


**I know I have two isprimes, because when I found that the program was getting stuck there I looked one up on the internet to see if it was a problem in my code. I have the same problem with either function, though. The first one is mine, the second one is one I found.

I tried compiling your program and got these warnings

In function ‘double lcm(double, double)’:
35: warning: no return statement in function returning non-void
In function ‘bool isprime(long int)’:
47: warning: control reaches end of non-void function
At global scope:
10: warning: ‘dividecounter’ defined but not used

lcm should be returning a double and isprime should be returning a bool.

Both isprime functions do return a boolean value... It looks like I did forget to place a return statement in lcm though. But the problem is occurring in isprime, and that function does have a return value

I'm wondering...can you call this function and have fail on all the if conditions? What does it return in that situation? It should have a default returning true or false.

bool isprime(long denominator)
{
 long max = long(sqrt(denominator));
 if(denominator <= 1)
  { return true; }
 if(denominator % 2 == 0)
  { return false; }
 for( long counter = 2; counter <= max; counter++)
  if(denominator % counter == 0)
   { return false; }
}

Well. Just by adding a

else
 return true;

statement at the bottom of that function, it works. But, why, exactly?

I just looked at your program...Your using a double for decimal, are you aware of the the storing characteristics of this type? Floating types may not store an exact value but a close approximate. This could be your problem.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.