Thread: keep getting two errors when working with fractions

1. keep getting two errors when working with fractions

Maybe someone can help me here too. The programm is meant to do basic calculation operations with fractions. Got everything done now, but I still get two errors when I use the programme.
Weirldy, some fractions dont make me problems at all, but on some the program just randomly stops doing anything in the division part, nothing happens anymore.
The other one is when I use the loop. I made it, that you can go for several loops until you tipe a "0" to stop the program. But when I go for the second loops it says Stack Overflow and I dont know why.

This is the main .c
Code:
```Code:
#include "rational.h"

int main(void)
{
int test;

do {
fraction1.denominator=0;
fraction1.numerator=0;
fraction2.denominator = 0;
fraction2.numerator = 0;
result.denominator = 0;
result.numerator = 0;
LMC = 0;

getch();
if (result.denominator < 0)
{
result.denominator = -result.denominator;
result.numerator = -result.numerator;
}
printf("Das Ergebnis der Addition beider Brüche ist:\n");
print_rational(result);

result = subtraction(fraction1, fraction2);
if (result.denominator < 0)
{
result.denominator = -result.denominator;
result.numerator = -result.numerator;
}
printf("Das Ergebnis der Subtraktion beider Brüche ist:\n");
print_rational(result);

result = division(fraction1, fraction2);
if (result.denominator < 0)
{
result.denominator = -result.denominator;
result.numerator = -result.numerator;
}
printf("Das Ergebnis der Division beider Brüche ist:\n");
print_rational(result);

result = multiplication(fraction1, fraction2);
if (result.denominator < 0)
{
result.denominator = -result.denominator;
result.numerator = -result.numerator;
}
printf("Das Ergebnis der Multiplikation beider Brüche ist:\n");
print_rational(result);

printf("Wenn sie aus der Schleife aussteigen wollen, geben sie '0' ein. Ansonsten etwas beliebiges anderes\n");
scanf_s("%d\n\n\n", &test);
} while (test != 0);

return 0;
}
```
here is the header, but there shouldnt be a problem
Code:
```Code:
#ifndef rational_h#define rational_h

#include <stdio.h>

typedef struct {
long numerator, denominator;
}rational;

rational fraction1;
rational fraction2;
rational result;
long LMC;

rational subtraction(rational, rational);
rational multiplication(rational, rational);
rational division(rational, rational);
void print_rational(rational);
long ggT(long, long);
long ggT1(long, long);

#endif
```
and here is the .c for the module. there is the division part also where the program stops. and when I get the other error with stack overflow its at the ggT function (at least there is where the cursor stops
Code:
```Code:
#include "rational.h"

{

result.numerator = fraction1.numerator + fraction2.numerator;
result.denominator = fraction1.denominator;
if (result.numerator < result.denominator)
{
LMC = ggT1(result.numerator, result.denominator);
}
else
{
LMC = ggT1(result.denominator, result.numerator);
}

result.numerator /= LMC;
result.denominator /= LMC;
return result;
}

rational subtraction(rational fraction1, rational fraction2)
{
result.numerator = fraction1.numerator-fraction2.numerator;
result.denominator = fraction1.denominator;
if (result.numerator < result.denominator)
{
LMC = ggT1(result.numerator, result.denominator);
}
else
{
LMC = ggT1(result.denominator, result.numerator);
}

result.numerator /= LMC;
result.denominator /= LMC;
return result;
}

rational multiplication(rational fraction1, rational fraction2)
{
result.numerator = fraction1.numerator * fraction2.numerator;
result.denominator = fraction1.denominator*fraction2.denominator;
if (result.numerator < result.denominator)
{
LMC = ggT1(result.numerator, result.denominator);
}
else
{
LMC = ggT1(result.denominator, result.numerator);
}

result.numerator /= LMC;
result.denominator /= LMC;

return result;
}

rational division(rational fraction1, rational fraction2)
{

result.numerator = fraction1.numerator * fraction2.denominator;
result.denominator = fraction1.denominator*fraction2.numerator;

if (result.numerator < result.denominator)
{
LMC=ggT1(result.numerator, result.denominator);
}
else
{
LMC = ggT1(result.denominator, result.numerator);
}

result.numerator /= LMC;
result.denominator /= LMC;

return result;
}

long ggT(long a, long b)
{
static long helper = 0;
helper += b;

if (helper%a == 0 && helper%b == 0)
{
return helper;
}
else
{
return ggT(a, b);
}

}

long ggT1(long a, long b)
{
int r = 0;

if (a == 0)
{
return b;
}
while (b != 0)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}

{
printf("Geben sie den Zaehler des ersten Bruchs ein\n");
scanf_s("%x", &fraction1.numerator);

printf("Geben sie den Nenner des ersten Bruchs ein\n");
scanf_s("%x", &fraction1.denominator);

printf("Geben sie den Zaehler des zweiten Bruchs ein\n");
scanf_s("%x", &fraction2.numerator);

printf("Geben sie den Nenner des zweiten Bruchs ein\n");
scanf_s("%x", &fraction2.denominator);

LMC=ggT(fraction1.denominator, fraction2.denominator);
fraction1.numerator *= (LMC / fraction1.denominator);
fraction1.denominator = LMC;

fraction2.numerator *= (LMC / fraction2.denominator);
fraction2.denominator = LMC;

return fraction1;
}

void print_rational(rational result)
{
printf("%ld/%ld\n\n", result.numerator, result.denominator);
}
```

2. You asked this question on your other thread, let's keep it there -> keep getting an to me unknown error