Turning user input into an array

Hi everyone ,

I am learning c++ by myself and just entered the topic of arrays .

I have a question about turning the user input into an array . The objective of my code is to read an input number from that user chooses and turn it into an array by spliting the number into integers and storing them separetly in array elements . For example :

User enters :1234

And the program will convert this into :
myarray[0]=1
myarray[1]=2
myarray[2]=3
myarray[3]=4

also by reading the number, the program has to make the array the size of the separate integers of the number , in this case 3.

is this possible ? , ive researched a lot about and nothing has helped me yet, it would be great if you guys can help me figure this one out .




Arrays have a fixed size determined before the program runs. You want to use a std::vector instead:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector

The array data type is not very useful in C++.
Last edited on
As LB said, static arrays must have a size at compile time. However, dynamic arrays are able to be allocated from the heap at run-time with a, well, dynamic size. I'd do something like this:

1) Request user input.
2) Store user input in a std::string
3) Create a dynamic array of size string.length().
4) Iterate through the string, character by character
5) Convert each character to their int counterpart
6) Store the int in the dynamic array.

I'd read up on Dynamic arrays before vectors as vectors are a parameterized class built on a dynamic array data structure. So, you might understand vectors better if you first study dynamic arrays.

http://www.cplusplus.com/doc/tutorial/dynamic/
It is generally a good idea to lean about vectors before arrays and dynamic memory.
Topic archived. No new replies allowed.