Patryk
Patryk

Reputation: 24092

Why for loop in C only passes once?

I have a really simple piece of code that checks if a number given is a permutation

(number of digits < 8; each digits shows up only once)

From unknown reasons to me for loop with marked comment ( HERE ) passes only once. Can anybody help me on that ?

#include <stdio.h>
#include <stdbool.h>

int num_digits(int number)
{
    int digits = 0;
    while (number) {
        number /= 10;
        digits++;
    }
    return digits;
}

int get_n_digit(int number, int which)
{
  if(which > num_digits(number))
  {
    printf("This number %d does not have that many digits: %d\n",number, which);
    return -1;
  }

  int to_return = number;
  int i = 0;
  for(i = 0; i < num_digits(number) - which;i++)
  {
    int digit = number % 10;
     to_return /= 10;
  }
  printf("get_n_digit(%d,%d)=%d\n",number,which, to_return %10);
  return to_return % 10;
}

bool permutation_check(int number)
{
  bool to_return = true;
  if(num_digits(number) > 8)
  {
    printf("Your number has more than 8 digits\n");
    return false;
  }
  int temp[10];
  int i;
  for( i = 0; i < 10; i++)
  {
    temp[i]=0;
  }

  printf(" for( i = 0; i < num_digits(%d) =  %d; i++)\n",number, num_digits(number));
  for( i = 0; i < num_digits(number); i++);
  {
    //HERE
    printf("temp[get_n_digit(%d,%d)]++;\n",number,i);
    temp[get_n_digit(number,i)]++;
    if( temp[get_n_digit(number,i)] > 1)
    {
      to_return = false;
    }
  }

  for( i = 0; i < 10; i++)
  {
    printf("%d\n",temp[i]);
  }
  return to_return;
}

int main(void)
{
  char line[256];
  int i, a;
  printf("Put a number to be checked for permutation condition.\n");
  if (fgets(line, sizeof(line), stdin)) {
      if (1 == sscanf(line, "%d", &i)) {
        printf("This many digits in this number: %d\n", num_digits(i));
        if(num_digits(i)<8)
        {
          if(permutation_check(i))
            printf("This number is a permutation.\n");
          else
            printf("This number is NOT a permutation.\n");
        }
   }
  }
  return 0;
}

Upvotes: 0

Views: 186

Answers (2)

djechlin
djechlin

Reputation: 60758

Firstly please compile with -Wall -Werror -Wextra. In particular this one is caught by -Wempty-body, which is enabled by -Wextra but not -Wall. You wouldn't be able to make this mistake at all.

The best way to discover what's wrong with your code is to precisely understand what it's doing. That's what debuggers are for. You can do this in your IDE and if you are not using an IDE the tool to use on *nix is gdb.

  1. Compile your code with -g flag.
  2. Run as e.g. gdb a.out.
  3. Set a breakpoint in main - (gdb) break main
  4. Run - (gdb) run

Step through your code and see exactly what it is doing, and where it is violating your expectations. In Eclipse double-click the line number next to the main function to set a breakpoint, and run by clicking the debugging symbol, which is probably a graphic of an insect. Visual Studio C++ I imagine will be similar.

In your case you would want to set a breakpoint on the line your for loop is on.

gdb break <line #>

Then next your way through. For a simple code file like this it will likely be immediately apparent what's going on - or at least you will have a far, far more specific problem to work with.

Appropriateness of this answer covered on meta.

Upvotes: 4

Joe Runde
Joe Runde

Reputation: 253

You have a semicolon at the end of your for statement

for( i = 0; i < num_digits(number); i++); // <-- Semicolon

Upvotes: 16

Related Questions