Author Topic: Compile Problem  (Read 4167 times)

Offline dsdeiz

  • Single posting newcomer
  • *
  • Posts: 2
Compile Problem
« on: March 28, 2010, 11:43:24 am »
Hello, I've got this really weird problem.

Code
#include <stdio.h>

int gcf(int a, int b) {
  int tmp;

  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }

  if (a % b == 0) {
    return b;
  }
  else {
    gcf(b, (a % b));
  }
}

int lcm(int a, int b) {
  return (a / gcf(a, b)) * b;
}

int main(int argc, char *argv[]) {
  int a, b, x, y;

  printf("Enter integer 1: ");
  scanf("%d", &a);

  printf("Enter integer 2: ");
  scanf("%d", &b);

  x = gcf(a, b);
  y = lcm(a, b);

  printf("\nGreatest common factor: %d\n", x);
  printf("Least common multiple: %d\n", y);

  return 0;
}

In Linux, doing gcc -o program program.c works just fine. But I'd also like to compile it for Windows so I tried Code Blocks. A sample input of 12 and 36 works just fine but an input of 18 and 84 gives a result of 2009155360 and 0.  :? Any things I should consider? Looking forward to your replies.

Thanks!

Offline GeO

  • Multiple posting newcomer
  • *
  • Posts: 51
Re: Compile Problem
« Reply #1 on: March 28, 2010, 11:49:38 am »
Shouldn't it be:

Code
  else {
    return gcf(b, (a % b));
  }

And 18 and 84 returns me 6 and 252, which should be right.

Attention, this has nothing to do with Code::Blocks, please ask this question on a c/c++ forum.

greets GeO
« Last Edit: March 28, 2010, 11:59:54 am by GeO »

Offline dsdeiz

  • Single posting newcomer
  • *
  • Posts: 2
Re: Compile Problem
« Reply #2 on: March 28, 2010, 11:52:00 am »
Oh right. My bad.. Already updated the code. Thanks! Though I still have the same problem.  :(

Offline koso

  • Multiple posting newcomer
  • *
  • Posts: 58
Re: Compile Problem
« Reply #3 on: March 28, 2010, 12:14:11 pm »
Have you tried to debug? At least you should try some debug console output and check input values in functions, also check user input -> thats I think only place dependent on platform. 

PS: that code is not very safe. Lcm function is vulnerable to "division by zero" error. 

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Compile Problem
« Reply #4 on: March 28, 2010, 12:53:37 pm »
This problem is not C::B related.
Most likely a problem with formatting your input (or output) and might be related to your system 32- aor 64-bit.

Nevertheless your post violates our forum rules, because the C::B forums are not related to general programming questions, so the topic gets locked.