Errors with Reducing a fraction to its simpliest form

EDIT*
I figured it out and got my code to work. It simplifies the fraction correctly, only problem now is it prompts the user to input a fraction twice
Welcome to fraction simplifier

You will be asked to enter a fraction in the form     integer / integer

The program will then attempt to calculate and display
 the simplified form of the fraction

Enter a fraction in the form (x/y): (For example, 20/100)

3/9
Enter a fraction in the form (x/y): (For example, 20/100)

4/16
The original fraction you entered was 4 / 16 

The simplified version of the fraction is 1 / 4 



Im writing a program to simplify a rational fraction to its simpliest form. I have my code completed and running with no errors, but its doing some weird things..printf and scanf are new to me so Im not sure who else to rearrange or change my code..

Im think the errors have to do with either one of these two functions i wrote:

1
2
3
4
5
6
7
8
9
10
11
Fraction getFraction()
{
   int numer, denom;
   Fraction userFraction;
   
   printf("Enter a fraction in the form (x/y): ");
   printf("(For example, 20/100)\n\n");

   scanf("%ld/%ld", &userFraction.numer, &userFraction.denom);   
   return userFraction;
}


or
1
2
3
4
5
6
7
8
9
10
11
12
Fraction simplify(Fraction f)
{
   long d, numer, denom;
   
   Fraction result;
   
   d = gcd(f.numer, f.denom);
   result.numer = (f.numer/d);
   result.denom = (f.denom/d);
   
   return result;
}


For my GCD function, I pretty much copied it from an existing one online, but maybe it isnt functioning properly?

1
2
3
4
5
6
7
8
9
10
11
long gcd(long x, long y)

   if(y == 0)
   {
      return x;
   }
   else
   {
      return gcd(y, x % y);
   }
}


Some direction on what is causing the errors would be awesome!!
Last edited on
Topic archived. No new replies allowed.