outputs number digit by digit

This is a discussion on outputs number digit by digit within the C++ Programming forums, part of the General Programming Boards category; hi all. i want to output to the screen a number. long int counter; i don't want to do it ...

  1. #1
    Unregistered
    Guest

    outputs number digit by digit

    hi all.

    i want to output to the screen a number.

    long int counter;

    i don't want to do it with

    cout<<counter;

    istead i want to use a

    int matrix[16];

    each matrix[i] will have a digit of counter.
    but i cannot make the programm work. this is my program

    #include<iostream.h>

    void main()
    {
    long int counter=464272572;
    int mat[12],i;

    i=0;
    while(counter!=0)
    {
    mat[i]=((float)(counter/10)-(int)(counter/10));
    counter/=10;
    i++;
    }

    while(i!=0)
    {
    cout<<mat[i];
    i--;
    }
    }
    can anyone help? thanks.

    regards, bill

  2. #2
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    im not sure what the hell your question is but, if you want to ouput an integer one digit at a time ( regardless of what stop they are in the int array) just convert it with itoa() and then output with putch();
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  3. #3
    Unregistered
    Guest
    hi.

    i forgot to say that i do not want to do it with ltoa.
    i just want to do divide the number each time with ten.

    regards

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >(float) (counter / 10) - (int) (counter / 10)
    This will always be zero!

    Your logic behind using i in the second while loop is slightly flawed. It will start one too high, because thats how the previous loop left it. It needs decrementing first.

    >void main()
    use int main(void)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    If I understood you correctly, you want to pass a long into an int array digit by digit. I did the following:

    Note: I'm far from a good C/C++ programmer. I'm still learning the language. So it's possible there is an easier way...
    Also the code may as well not be clean... I like to double check and triple check my loops when I have the time. I like small loops
    [EDIT] I mean functions, not loops. Particularly the recursive one. I'm sure it could be optimized [/EDIT]

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int digits_lng(long number) //I use a recursive function here
    {
        static int counter=0;
        static long temp=number;
        if(temp < 1) return counter;
        temp /= 10;
        counter++;
        digits_lng(temp);
    }
    
    //Main
    int main()
    {
        long num = 45634253;
        int numdigits = digits_lng(num); //I'm saving this cause we'll need it later
        int mat[numdigits-1]; //this way you can build your arrays with the correct size
        
        for(int x=numdigits;x>=1;x--){
            mat[x-1] = num % 10; //note that I choose the modulos operator here.
            num /= 10;}
    
        
        for(int i=0;i<=numdigits-1;i++) cout << mat[i];
        
        cout << "\n\n";
        system("PAUSE");
        return 0;
    }
    Last edited by Mario; 05-17-2002 at 10:39 AM.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Originally posted by Mario

    >int numdigits = digits_lng(num);
    >int mat[numdigits-1];
    You can't do things like this. At least my compiler won't let you any I'm sure c++ doesn't allow for it.

    It's to do with the way you've initialised the number of elements in the mat array, it needs to be known at compile time. Easiest way round this is to not try and be too clever, just make the array a fixed size, long enough to hold any long int.

    Also, your function digits_lng() only works once. Call it again with another number and it returns the same result as the first time. This is because the static values only get initialised once. So this bit of code:
    >static int counter=0;
    >static long temp=number;
    only happens once, and screws up further calls.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by Hammer
    >Originally posted by Mario

    >int numdigits = digits_lng(num);
    >int mat[numdigits-1];
    You can't do things like this. At least my compiler won't let you any I'm sure c++ doesn't allow for it.
    I'm not sure what you are talking about. It compiled nicely and I'm using what you see on my signature. I can do that! doh!

    Also, your function digits_lng() only works once. Call it again with another number and it returns the same result as the first time. This is because the static values only get initialised once. So this bit of code:
    >static int counter=0;
    >static long temp=number;
    only happens once, and screws up further calls.
    What da?? Have you actually ran the code or are you just talking over your shoulder? I don't post before compiling and running my code. The above works simply because those vars are NOT being re-declared. They are inside a function that is being called over and over.... If you have a function being called in your main twice, does that mean you can no longer declare a static variable inside your function? Yeah... I though so.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Mario

    What da?? Have you actually ran the code or are you just talking over your shoulder? I don't post before compiling and running my code. The above works simply because those vars are NOT being re-declared. They are inside a function that is being called over and over.... If you have a function being called in your main twice, does that mean you can no longer declare a static variable inside your function? Yeah... I though so.
    Yes I ran your code Change your main() to this and run it. Your function doesn't work a second time.
    Code:
    int main()
    {
        cout <<digits_lng(123456) <<endl;
        cout <<digits_lng(2) <<endl;
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Yes! I can see what you mean. It's the 'count' var that is messing with the final result. Hmm....

    I could copy it into a temp var and reset it before the return. But i find it rather ugly:

    Code:
    int digits_lng(long number) //I use a recursive function here
    {
        int result;
        static int counter=0;
        static long temp=number;
        if(temp < 1){
            result = counter;
            counter = 0;
            return result;}
        temp /= 10;
        counter++;
        digits_lng(temp);
    }
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You don't need to go all recursive, just try this:
    Code:
    int digits_lng(long number)
    {
    	int count = 0;
    	while (number >= 1L)
    	{
    		number /= 10L;
    		count ++;
    	}
    	return (count);
    }
    I think that'll work OK.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    LOL!

    That really made my code look ridiculous. Thanks for the humbling lesson

    Just one thing though. Now, that I can see you were not pulling my leg back there, what did you mean by code like int array[int var] not being valid?
    It did compile with me. But, I don't want to produce bad code...
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >That really made my code look ridiculous. Thanks for the humbling lesson
    That's OK, glad ya take it well

    >what did you mean by code like int array[int var] not being valid?
    Well, I'm better at C than C++, but I thought that the array size had to be a constant, and therefore known at compile time (it certainly has to be that way in C). When I compile your code under Borland 5.5 (a c++ compiler), I get
    >Constant expression required in function main()

    Now, taking note of what you said earlier, I tried compiling it with gcc, and it worked (or at least appeared to).

    >I don't want to produce bad code...
    If I were you, I'd check this one out with a c++ expert. There's definately something weird with it. Maybe some kind sole will read this post and answer the question.

    Happy coding!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    int num = 456;
    int nums[3];
    int i;
    int count = 0;
    int temp;
    
    //strip digits from right to left 1 at a time and store array
    while(num > 0)
    {
       nums[i] = num % 10;
       num = num/10;
       count++;
    }
    
    //reverse array so digits in correct order
    for(i = 0; i < count/2; i++)
    {
       temp = nums[i];
       nums[i] = nums[count - i];
       nums[count - i] = temp;
    }
    I agree, but am no expert, that the standard is an array's size is a constant when declared at compile time on the stack using static memory. If compiler allows a variable to be used it's wrong, but it may work.
    Last edited by elad; 05-17-2002 at 03:57 PM.

  14. #14
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    According to Herbert Schildt (in C/C++ Programmer's Reference - Osborne) the general form for an array declaration is:

    type var-name[size]

    He's very strict on the way he presents code syntax. So i'm inclined to accept that an array needs a constant for it's size. Since i'm still learning, I got carried away by my previous VB experience and when it worked, i didn't even bother.

    That GCC allows this seems to me a bad call since this is a fundamental aspect of code syntax. Something compilers shouldn't mess with.

    BTW, just for the fun of it, I initiated a C project under dev-cpp and it still allowed me a variable in place of a constant
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Since i'm still learning
    We all are...... and always will be!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. how to read a digit of a floating point number?????
    By spicy_centipede in forum C Programming
    Replies: 15
    Last Post: 07-14-2007, 11:43 AM
  3. 4 digit number
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 06-21-2005, 04:41 AM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Replies: 4
    Last Post: 07-14-2003, 08:11 AM

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