Thread: Function that should return number of digits in an integer returns last digit.

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Function that should return number of digits in an integer returns last digit.

    Title. Can someone help me fix it?
    Code:
    int exploder(int number,int array[]) {
        int functi = 0;
        int digit = number % 10;
        while (number > 0) {
         //   int digit = number % 10;
            array[functi] = digit;
            functi++;
          //  cout << digit << '\n';
            number /= 10;
        }
        return digit;
        
    }

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    How is this supposed to return the number of digits in a number? It's just a matter of looking at what you are returning...
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    This is where using meaningful variable names (wtf is functi?) comes in handy.
    Code:
    int exploder(int numberToExplode,int arrayOfDigits[]) {
        int numberOfDigits = 0;
        int currentDigit = numberToExplode % 10;
        while (numberToExplode > 0) {
         //   int currentDigit = numberToExplode % 10;
            arrayOfDigits[numberOfDigits] = currentDigit;
            numberOfDigits++;
          //  cout << currentDigit << '\n';
            numberToExplode /= 10;
        }
        return currentDigit;
         
    }
    Can you see now what to return?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion to find sum of digits of n digit number ?
    By justine in forum C Programming
    Replies: 7
    Last Post: 11-26-2012, 05:35 AM
  2. sum of digits of a number reducing to 1 digit.
    By amolkarale in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 05:55 PM
  3. five digit number with different digits
    By khdani in forum C Programming
    Replies: 11
    Last Post: 05-03-2009, 11:33 AM
  4. To get the sum of digits of a five digit number
    By hum_tum2005007 in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 04:39 PM
  5. Replies: 2
    Last Post: 12-07-2004, 02:31 AM