# Thread: outputs number digit by digit

1. ## 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. 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();

3. 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. >(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)

5. 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;
}```

6. >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.

7. 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.

8. 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;
}```

9. 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);
}```

10. 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.

11. 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...

12. >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!

13. 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.

14. 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

15. >Since i'm still learning
We all are...... and always will be!

Popular pages Recent additions