Thread: Unusual Counter Behavior After 10 Digits?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11: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, 01:33 AM
  4. can you help me please?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2002, 09: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