Input number as string in an array.

I need to ask the user to input a number, and then put the number in a string array. so for example if the size of the number was 4: example 1200

i would make him put the number like that

for (i1=0; i1 < 4; i1++)
cin >> Num1[i1];
where Num1[] is a char array type. yes and it works. but my question is what if I do not know how many numbers will the user put, how do i extract the size of the total characters existing in a number?

Please i really need some help
Last edited on
there might be an easier way to do it, take the input to a variable of int type, count the number of digits,lets say its n , create dynamically a char array of size n and then copy each digit into the appropriate index of the char array

int num,n=0;

int x=num;
{ int d=x%10;

thats just the pseudo for knowing the number of digits, n will give you the number of total characters existing in the number ... hope you can understand the logic
Last edited on
you can use sprintf and get the string length
@Codefisher sure this works only thing is the whole idea of the problem is to store an integer that might be bigger than the largest value that can be placed into integer and that is 2147483647. so the user will not be able to enter the cin >>num that you are talking about
Okay i got it first to store numbers as string
so string integer1;
cin >> integer1;

now how do i store that string into an array ?
by array here you mean character array right?
Topic archived. No new replies allowed.