Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: dsdeiz on March 28, 2010, 11:43:24 am

Title: Compile Problem
Post by: dsdeiz 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!
Title: Re: Compile Problem
Post by: GeO 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
Title: Re: Compile Problem
Post by: dsdeiz on March 28, 2010, 11:52:00 am
Oh right. My bad.. Already updated the code. Thanks! Though I still have the same problem.  :(
Title: Re: Compile Problem
Post by: koso 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. 
Title: Re: Compile Problem
Post by: Jenna 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.