Click here to Skip to main content
15,920,614 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working on a project in c where I need to store digits in an array.
I'm facing a problem in storing a digit which begins with a 0. i have digits like 00123 04567 000012 etc i want to store these digits in an array in c..im using code blocks..but the digits that begin with zero are replaced by some other digits in the output..i declared the array as integer..can i use any other data type?
Posted
Comments
CHill60 10-Apr-14 13:54pm    
If you post your code we might have more chance of helping
[no name] 10-Apr-14 20:34pm    
At present question is not answerable. 002567 is not a digit. Use "Improve question" to add more information and/or code.

Correct me if I'm wrong, but it sounds like your trying to store a number in an array, with each element representing a different digit.

So for instance, 01234 would look something like

C++
int DigitArr[] = 
{ 
    0, 1, 2, 3, 4
};


Here's my question, how are you assigning the digits to the array? I think this might be where you problem is.

Also, when you print the digits out, make sure you're printing them as ASCII characters. Try either casting them to chars, i.e.
C++
(char)DigitArr[0];


Hope this helps a little.
Jacob
 
Share this answer
 
v2
That is (00123 04567 000012) not a number. In computational sense a number cannot have 0s in the front (Try inserting 001 into a calculator).

So that is a sequence of digits (In a fixed length I suppose). You cannot treat it as a number but you can treat it as an array of digits. I suppose you want to store a multiple number of these sequences. So do it this way :

C++
const size_t length = 16; //number of digits in a one sequence

int* numbersArray[10]; // an array of ten integer pointers
size_t numUsed = 0; //number of uesd elements in the numbersArray

//a few sequences of 16 digits
int one[length] = {0,0,0,0,2,3,4,5,6,7,8,9,0,1,2,4};
int two[length] = {0,0,5,6,7,8,3,2,1,6,3,4,5,3,2,7};
int three[length] = {0,2,3,5,3,5,6,7,3,2,6,3,5,3,5,3};

numbersArray[0] = one;
numbersArray[1] = two;
numbersArray[2] = three;

numUsed = 3;

//you can iterate through numbersArray like this:
int* current;
for(int i=0; i < numUsed; i++)
{
   current = numbersArray[i];

   for(int j=0; j < length; j++)
   {
      std::cout << current[j];
   }

   std::cout << endl;
}



It gets somewhat complicated if your "numbers" are not in a fixed length. Anyway, I hope you got what you wanted !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900