# Unusual Counter Behavior After 10 Digits?

• 07-08-2008
parx86
Unusual Counter Behavior After 10 Digits?
I've written a n00b practice program that extracts each digit from an integer of any size, and tell you how many of those integers are the number 7. The program works great, as long as the number the user enters is 9 digits or less...once it goes over that, the amount of 7's the program counts gets weird. Haven't been able to find anyone else with this exact problem, so any understanding of this issue would be greatly appreciated. Here's what I've got:

Code:

```/*This program will tell you how many digits in your number are 7*/ #include <stdio.h> int main() {     int number, number_counter = 0, digit, digit_counter = 0;         printf("\nPlease enter a number: ");     scanf("%d", &number);         /*So we can always refer back to the original number if needed, and work with number_counter instead*/     number_counter = number;         /*number_counter will be less than one only after the last digit is extracted*/     while(number_counter > 1){                                                 /*This sets digit to the furthest remaining to the right digit*/                         digit = number_counter % 10;                                                 /*This removes the digit we just extracted*/                         number_counter /= 10;                                                 /*This keeps a count of how many 7's have been found so far, for some reason it will only print a single digit?*/                         if(digit == 7){                                   digit_counter++;                         }     }         /*This will only print a single digit, ie. 10 == 0, couldn't figure this out?*/     printf("\n %d of the numbers you entered were 7!\n\n", digit_counter);         return 0; }```
I had thought maybe I was running into a size limit, so I tried setting my variables to long and long long, but that had no effect on the problem, so I'm stumped from here :)
• 07-08-2008
laserlight
Quote:

The program works great, as long as the number the user enters is 9 digits or less...once it goes over that, the amount of 7's the program counts gets weird.
The problem is probably because with more than 9 digits, the input cannot fit into an int. The simple solution to that is to read in the input as a string, but then that might defeat the purpose of the exercise, so perhaps you should just let it be and accept it as a limitation.

Quote:

I had thought maybe I was running into a size limit, so I tried setting my variables to long and long long, but that had no effect on the problem, so I'm stumped from here
It could be the case that int and long are of the same size and range on your system.
• 07-08-2008
parx86
Quote:

perhaps you should just let it be and accept it as a limitation
I had also thought about that, since I'm only in chapter 3 of the book I'm in, doing lots of while() loops and such, that maybe I might be getting ahead a bit, so unless anyone else has any ideas, I'll accept that as my answer :)