views:

855

answers:

6

I have a for loop generating integers.

For instance:

for (int i=300; i>200; i--)
    {(somefunction)*i=n;
    cout<<n;
    }

This produces an output on the screen like this:

f=00000000000100023;

I want to store the 100023 part of this number (i.e just ignore all the zeros before the non zero numbers start but then keeping the zeros which follow) as an array.

Like this:

array[0]=1;
array[1]=0;
array[2]=0;
array[3]=0;
array[4]=2;
array[5]=3;

How would I go about achieving this?

A: 

An integer will not store zero's that come before a value (leading zero's)

int i = 0001199;

i will equal 1199

If this is a string rather than an int, then you can use the property of ints not to store leading zero's.

Cast from string to int Cast back from int to string

e5
+2  A: 

The straightforward way would be

std::vector<int> vec;
while(MyInt > 0)
{
  vec.push_back(MyInt%10);
  MyInt /= 10;
}

which stores the decimals in reverse order (vector used to simplify my code).

stefaanv
push_front would be better, but I normally use push_back with vectors
stefaanv
+3  A: 

In light of the edited question, my original answer (below) isn't the best. If you absolutely have to have the output in an array instead of a vector, you can start with GMan's answer then transfer the resulting bytes to an array. You could do the same with JohnFx's answer once you find the first non-zero digit in his result.


I'm assuming f is of type int, in which case it doesn't store the leading zeroes.

int f = 100023;

To start you need to find the required length of the array. You can do that by taking the log (base 10) of f. You can import the cmath library to use the log10 function.

int length = log10(f);
int array[length];

length should now be 6.

Next you can strip each digit from f and store it in the array using a loop and the modulus (%) operator.

for(int i=length-1; i >= 0; --i)
{
    array[i] = f % 10;
    f = f / 10;
}

Each time through the loop, the modulus takes the last digit by returning the remainder from division by 10. The next line divides f by 10 to get ready for the next iteration of the loop.

Bill the Lizard
bill thats a great answer but i made a mistake saying the number was an integer. sorry! thanx you for your help.
question has now been corrected
+1  A: 

Since the leading zeros are not kept because it represents the same number

See: http://stackoverflow.com/questions/515612/convert-an-integer-number-into-an-array

TStamper
+2  A: 

Hang on a second. If you wrote the code generating the integers, why bother parsing it back into an array?

Why not just jam the integers into an array in your loop?

int array[100];

for (int i=300; i>200; i--)    
{
    array[i]= (somefunction)*i;    
}
JohnFx
it is necessary to get rid of the preceeding zeros as the number required will only be of value if it is the correct order, hope that helps
I still think this will work. The leading zeros in an integer are just logical. If they are showing up from your programming that is just a formatting issue not a storage issue. In fact, it takes EXTRA work to get leading zeros to show up like that. The code you provided in the original question would not produce the output you described.
JohnFx
After looking at your code some more, I think you might be confusing what cout displays and what is actually stored. You might have, for example, setfill('0') somewhere in the code that will pad the number when printed out via COUT.
JohnFx
@noob09: I would just do what JohnFX has here, then loop through the array at the end to find the first non-zero digit, get the size of the result from that, then copy the remaining digits into a new array.
Bill the Lizard
Why loop through the array?At the end of the code I posted @noob09 should have the final output he was looking for with no additional work required.
JohnFx
@JohnFx: You'll have all 100 digits in your array, including leading zeroes. The extra loop is just to find the first non-zero digit and return the remainder of the array.
Bill the Lizard
I was assuming that (somefunction) returned an integer. Are you thinking he meant it returned a single character?
JohnFx
thanx for your hel guys !!!
+3  A: 

This is a mish-mash of answers, because they are all there, I just don't think you're seeing the solution.

First off, if they are integers Bill's answer along with the other answers are great, save some of them skip out on the "store in array" part. Also, as pointed out in a comment on your question, this part is a duplicate.

But with your new code, the solution I had in mind was John's solution. You just need to figure out how to ignore leading zero's, which is easy:

std::vector<int> digits;
bool inNumber = false;

for (int i=300; i>200; i--)    
{
    int value = (somefunction) * i;

    if (value != 0)
    {
        inNumber = true; // its not zero, so we have entered the number
    }

    if (inNumber)
    {
        // this code cannot execute until we hit the first non-zero number
        digits.push_back(value);
    }
}

Basically, just don't start pushing until you've reached the actual number.

GMan