Error in C Program (not fully exiting loop?) -


this program supposed read digits of number entered user , check repeated digits. program keeps asking numbers until user enters less or equal 0. works in sense if enter repeated digit, tell you. if enter same digit number call, considered repeat digit. example, if 23 entered in 1 instance , 52 entered in another, considers there duplicate (even though there isn't in new number entered). ideas on how fix it?

#include <stdio.h> #include <stdbool.h> //as per c99 standard  int main (void) {  bool digit_seen[10] = {false}; int digit; long n;  while (1){      printf("enter number (enter 0 terminate program): ");     scanf("%ld", &n);      if (n == 0){     break;     }      while (n > 0){         digit = n % 10;          if (digit_seen[digit]){             break;         }          digit_seen[digit] = true;         n /= 10;     }      if (n > 0){         printf("repeated digit\n");     } else {         printf("no repeated digit\n");     } }  return 0; } 

however if enter same digit number call, considered repeat digit.

as stands, digit_seen initialised once , outside while loop, never reinitialised new input n.

you need move code bool digit_seen[10] = {false}; inside while loop. fix issue.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -