Unusual Counter Behavior After 10 Digits?

This is a discussion on Unusual Counter Behavior After 10 Digits? within the C Programming forums, part of the General Programming Boards category; I've written a n00b practice program that extracts each digit from an integer of any size, and tell you how ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    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.

    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    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

    Thanks again for your insight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 10:00 AM
  2. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 08:54 AM
  3. Formatting Output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-26-2002, 12:33 AM
  4. can you help me please?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2002, 08:21 AM
  5. Extracting individual digits from a short
    By G'n'R in forum C Programming
    Replies: 9
    Last Post: 08-30-2001, 10:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21