Thread: Writing program that prints out each digit of an entered number, loop question

  1. #31
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by Kelton2 View Post
    I got it to work with recursion, but it prints one too many comma and IDK how to fix it:
    Can someone help?
    This is a cosmetic issue. It's an issue an if can solve though. Then the number is first divided, you don't need to print a comma, but at all other times, you do.
    Last edited by whiteflags; 02-18-2015 at 11:26 AM.

  2. #32
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    Then the number is first divided
    How would I check for that?

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    One way is to turn the recursive function into a helper function which is called from your actual function. This actual function will not print the comma, but it does call the recursive function that does print the comma.
    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

  4. #34
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by laserlight View Post
    One way is to turn the recursive function into a helper function which is called from your actual function. This actual function will not print the comma, but it does call the recursive function that does print the comma.
    Isn't it already like that?

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote Originally Posted by Kelton2
    Isn't it already like that?
    No, at the moment you don't have a recursive helper function. Your explode function that only has one parameter is both the function called in main and the recursive function, whereas my suggestion is to make the function called in main non-recursive and not print the comma, and have that non-recursive function call the recursive helper function that prints the comma.
    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

  6. #36
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I have changed it back to iteration and I now have:
    Code:
    #include <iostream>
    using namespace std;
    int explode(int number,int array[])
    {   int variable = number;
        int numberOfDigits = 0;
        int digit = number % 10;
        array[digit] = digit;
        while (number > 0) {
            int digit = number % 10;
            int numbe = number;
             array[numberOfDigits] = digit;
            cout << array[numberOfDigits];
            number /= 10;
            numberOfDigits++;
        }
        int i = 0;
        int save = 0;
        save = numberOfDigits; 
        for(i = 0; i < numberOfDigits/2; i++) {
                array[i] = array[save--];
            //    cout << "Starting arrayprint:";
              //  cout << array[i] << endl;
                }
          //    cout << "End of arrayprint";
      //  return array[i];
       /* while (number > 0) {
            number /= 10;
            numberOfDigits++;
        } */
    
    
        return numberOfDigits;
    }
    /*int printDigits(int number,int array[])
    {
        int digit = number % 10;
        array[digit] = digit;
        while (number > 0) {
            int digit = number % 10;
            int numbe = number;
             array[digit] = digit;
            cout << array[digit];
            number /= 10;
        }
        int i = 0;
        for(i = 0; i < number/2; i++) {
                array[i] = number-i-1;
                }
        return array[i];
                
    } */
    
    
    int main ( ) {
        int number;
        int array[10];
        cout << "Enter number: ";
        cin >> number;
        cout << "[";
         explode(number,array);
         cout << ",";
         cout << "]";
    }
    It prints backwards. How do I fix it?

  7. #37
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Update: It still doesn't work, but I have updated my code. It now has a revised main and prints in a different way. My code is below:
    Code:
    
    
    Code:
    #include <iostream>#include <iomanip>
    #include <cmath>
    using namespace std;
    int explode(int number,int array[])
    {   int variable = number;
        int numberOfDigits = 0;
        int digit = number % 10;
        array[digit] = digit;
        
        while (number > 0) {
            int digit = number % 10;
            int numbe = number;
             array[numberOfDigits] = digit;
          //  cout << array[numberOfDigits];
            number /= 10;
            numberOfDigits++;
        }
        int i = 0;
        int save = 0;
        save = numberOfDigits; 
        for(i = 0; i < numberOfDigits/2; i++) {
                array[i] = array[i];
              //  cout << "Starting arrayprint:";
            //    cout << array[i] << endl;
                }
          //    cout << "End of arrayprint";
      //  return array[i];
       /* while (number > 0) {
            number /= 10;
            numberOfDigits++;
        } */
    
    
        return numberOfDigits;
    }
    /*int printDigits(int number,int array[])
    {
        int digit = number % 10;
        array[digit] = digit;
        while (number > 0) {
            int digit = number % 10;
            int numbe = number;
             array[digit] = digit;
            cout << array[digit];
            number /= 10;
        }
        int i = 0;
        for(i = 0; i < number/2; i++) {
                array[i] = number-i-1;
                }
        return array[i];
                
    } */
    
    
    int main()
    {
    	int digits[10];
    	int numdigits;
    	
    	int n;
    	cout << "Enter number: ";
    	cin >> n;
    	
    	numdigits = explode(n,digits);
    	cout << "The following is numdigits: " << numdigits;
    	cout << endl;
    	cout << "[" << digits[0];
    	for( int i = 1; i < numdigits; i++ )
    		cout << "," << digits[i];
    	cout << "]" << endl;
    }
    Can someone help?

  8. #38
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Nevermind, solved.

  9. #39
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    If you can figure out the number of digits the number has in advance, you can use a plain loop to store the numbers in correct order:

    Code:
    void extractDigits (int number, int* array, int sizeArray)
    {
        for (int i = std::min(sizeArray - 1, numberDigits(number)); i >= 0; i--) {
            array[i] = number % 10;
            number /= 10;
        }
    }
    As for how to get the number of digits a number will have, a big hint is to consider the underlined expression in the for loop above (number /= 10). This expression evaluates to zero when there are no more digits left in a number. You can use this to get the number of digits.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 01-09-2013, 06:35 AM
  2. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  3. Replies: 10
    Last Post: 09-24-2010, 01:09 AM
  4. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  5. Replies: 7
    Last Post: 05-26-2003, 05:44 PM