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

  1. #16
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    It's data (what's printed finally is data[i]). What do I need to change in it?

  2. #17
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Kelton2 View Post
    It's data (what's printed finally is data[i]). What do I need to change in it?
    That's good. I'm happy to hear that.

    Now, it's up to you to decide how to either re-write the data or print it in a different way.

  3. #18
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by MutantJohn View Post
    That's good. I'm happy to hear that.

    Now, it's up to you to decide how to either re-write the data or print it in a different way.
    I would rather print it differently, but how?

  4. #19
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I can't get this to work, please tell me what I am missing:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    
    int explode(int number, int array[])
    {
        int count = 0;
        int thing = 0;
     int digit = 0;
     int returner = 0;
     for(int i = 0; i < number; i++) {
              digit = number % 10;
              number = number / 10;
              
             thing += digit;
             returner = number % 10;
              }
              cout << digit;
    return returner;
    }
    
    
    int main()
    {
    	int digits[10];
    	int numdigits;
    	
    	int n;
    	cout << "Enter number: ";
    	cin >> n;
    	
    	numdigits = explode(n,digits);
    	
    	cout << "[" << digits[0];
    	for( int i = 1; i < numdigits; i++ )
    		cout << "," << digits[i];
    	cout << "]" << endl;
    }
    Last edited by Kelton2; 02-17-2015 at 06:23 PM.

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can't get this to work, please tell me what I am missing:
    You have to stop with these "doesn't work" posts. First of all, even if we want to help you, you're also dumping code and expecting everyone to figure out the problem AND the solution. It makes you look like you're lazy and don't care about your own work. We can't give you solutions that you can just hand in.

    You do realize that numbers are a sum of powers of ten right? 123456789 = 9*1 + 8*10 + 7*100 + 6*1000 + 5*10,000 + 4*100,000 + 3*1,000,000 + 2*10,000,000 + 1*100,000,000
    As you peel off digits you have to shrink the number by a power of ten. The code obviously starts with the smallest powers first.

    Go back to simpler code and ask yourself:
    1) How is the array storing the number?
    2) Where do I start in the array if I want to print back the number? You know that you can't start at 0; you've seen that it is wrong.

    You can fix this without make explode very complicated. Remember, the problem is the order of the output, not the output itself. If you can't figure out the array's workings, try one of laserlight's earlier suggestions.

  6. #21
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I figured it out, but it prints backwards:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    
    void explode(int number,int array[])
    {
        
        while (number > 0) {
            int digit = number % 10;
            cout << digit << '\n';
            number /= 10;
        }
    }
    
    
    int main()
    {
    	int digits[100];
    	int numdigits;
    	
    	int n;
    	cout << "Enter number: ";
    	cin >> n;
    	
    //	numdigits = explode(n,digits);
    	
    	cout << "[";
    	while (n > 0) {
            int digit = n % 10;
            n /= 10;
            digits[digit] = digit;
           // cout << digits[digit];
            
        }
    	cout << "]" << endl;
    }

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
     digits[digit] = digit;
    A digit has no real tie with where it is in the number. O_o What if the digit you peeled off was 1 in 90210. It doesn't go in digits[1], does it?

    The source of the error in all versions of the code is obvious. You keep saying that it prints backwards. You need to reverse the order of the digits you print. That is all. Nothing else is wrong.

  8. #23
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    A digit has no real tie with where it is in the number. O_o What if the digit you peeled off was 1 in 90210. It doesn't go in digits[1], does it?

    The source of the error in all versions of the code is obvious. You keep saying that it prints backwards. You need to reverse the order of the digits you print. That is all. Nothing else is wrong.
    And how would I reverse the order?

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Kelton2 View Post
    And how would I reverse the order?
    You're selling yourself short like this, I swear. :/

    It's in an array right? The array for 12345 looks like this:
    Code:
    { 5, 4, 3, 2, 1 }
    What do you have to do to print forward? 1 is in position 4, right?

    Keep trying.

  10. #25
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Yes, but it won't always be in that position. I printed digits[4] anyways, and it didn't work. I don't understand how I can get it to print in the right order.

  11. #26
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    
    int explode(int number,int array[])
    {
        int digit = number % 10;
        while (number > 0) {
            int digit = number % 10;
          //  cout << digit << '\n';
            number /= 10;
        }
        return digit;
        
    }
    
    
    int main()
    {
    	int digits[100];
    	int numdigits;
    	
    	int n;
    	cout << "Enter number: ";
    	cin >> n;
    	
    	numdigits = explode(n,digits);
    //	cout << numdigits << endl;
    	int array[numdigits];
    	cout << "[";
    	for(int i = 0; i < n; i++) {
            int digit = n % 10;
          //  n /= 10;
          //  digits[digit] = digit;
            cout << array[numdigits];
            
        }
    	cout << "]" << endl;
    }
    not working

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Kelton2 View Post
    Yes, but it won't always be in that position.
    The first digit of the number is always going to be in the last position. That was what I was trying to illustrate.

    I printed digits[4] anyways, and it didn't work.
    We have yet another case of you not thinking.

    I don't understand how I can get it to print in the right order.
    You understand how the number is being printed right now, right?

    You need to reverse this process. You should know how to do this yourself. Go back to the code where the only problem was that the number printed backwards and fix it.

  13. #28
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Kelton, for the love of freaking God, google, "How to print an array backwards in C".

  14. #29
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    You've declared 'digit' twice. As for the printing backwards, I like laserlights suggestion of using recursion for this. It seems the fastest way without using intermediate array memory. I dunno.

    just remember:
    Code:
     void try(){ try() };
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  15. #30
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I got it to work with recursion, but it prints one too many comma and IDK how to fix it:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int explode(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;
        
    }
    
    
    void explode(int number)
    {
         int ctrack = 0;
        if (number <= 0) return;
        int digit = number % 10;
        explode(number/10);
        while(ctrack < 1) {
        cout << digit; 
        ctrack++;
        cout << ",";
    }
    }
    
    
    
    
    int main()
    {
        int numdigits[100];
        int n;
        cout << "Enter number: ";
        cin >> n;
      //  int digit = explode(n,numdigits[]);
        cout<<"[";
         explode(n);
        cout<<"]\n";
    }
    Can someone help?

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