How to make an array which stores values from the user

How to make an array which stores the values i'm getting from the users?

For example if i have a function which allows the user to give me values, how can i store them in an array.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    int ary[ 10 ];

    for ( int i = 0 ; i < 10 ; ++i ) //gets user input
    {
        cin >> ary[ i ];
    }

    for ( int i = 0 ; i < 10 ; ++i ) //display input
    {
        cout << i+1 << ": " << ary[i] << endl;
    }
}


Do you mean something like that?
exactly, but if i want it for letters or words i should change the array type?
yes you would.

http://www.cplusplus.com/reference/istream/iostream/

alot of functions here can be helpful. std::getline is really great for string input
I think what you're looking for is dynamic memory allocation, they let you store values specified by the user, here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
int size;
cout<<"Please enter the size of the array.\n";
cin>>size;

int* array = new int[size];
for ( int  i = 0 ; i < size ; ++i )
{
cout<<"Enter the value of the element at position " <<i<< " of your array: ";
cin>>array[i];
}
cout<<"Here's the elements of your array: \n";
for ( int i = 0 ; i < size ; ++i )
{
cout<<array[i]<<endl;
}
return 0;
}
Topic archived. No new replies allowed.