Evelyn
Evelyn

Reputation: 43

dynamically allocated arrays with user input c++

I want to have a mini function that allows the user to type a group of numbers, and each of them will be dynamically allocated into an array. For example:

int main()
{
    int* x = NULL;
    int n, numbers;
    std::cin >> n;
    x = new int[n]
    for (int i=0;i<n;i++){
        std::cin >> numbers;
        x[i] = numbers;
    }

delete [] x;

So when the user types in

3

The user will be able to type in 3 numbers following that

3 1 2 3

I am trying to store the values 1, 2, 3 into an array so it will look like

[1, 2, 3]

but right now it's storing as

[123]

Anyway i can fix this? I'm new to C++ programming so I feel like there's an easy solution to this but i'm not sure how.. thank you!

Upvotes: 0

Views: 18591

Answers (4)

Zac Howland
Zac Howland

Reputation: 15872

Example 1

int main()
{
    int* x = NULL;
    int n, numbers;
    std::cin >> n;
    x = new int[n]; // need a semi-colon here
    for (int i=0;i<n;i++)
    {
        std::cin >> numbers;
        x[i] = numbers;
    }

    for (int j = 0; j < n; ++j)
    {
        std::cout << "x[" << j << "] = " << x[j] << std::endl;
    }

    delete [] x; // you mean x, not a

    return 0;
}

Once you fix (what I assume are just typos), what you have works fine. However, unless this is for an assignment, you should consider using std::vector instead of raw dynamic memory allocation. Doing so would reduce your code to about 4 lines.

int main()
{
    std::vector<int> myvector;
    std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(myvector));
    std::copy(myvector.begin(), myvector.end(), std::ostream_iterator<int>(std::cout, "\n"));
    return 0;
}

or, in C++11

int main()
{
    std::vector<int> myvector{std::istream_iterator<int>(std::cin),  std::istream_iterator<int>()};
    std::copy(myvector.begin(), myvector.end(), std::ostream_iterator<int>(std::cout, "\n"));
    return 0;
}

Upvotes: 2

user2033018
user2033018

Reputation:

You can use a vector instead:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> v;
    for (int i = 0; i < n; ++i)
    {
        int val;
        cin >> val;

        v.push_back(val);
    }
}

C++'s vector takes care of memory allocation for you. You could then simply traverse it and print its contents.

cout << "[";
for (int i = 0; i < v.size(); ++i)
{
    if (i != 0)
        cout << ",";
    cout << v[i];
}
cout << "]";

Upvotes: 2

suspectus
suspectus

Reputation: 17268

You could store each element of the array directly into x[i]. Not sure what numbers is used for (I've assigned numbers from x[i]).

x is the array that is to be deleted. And there is a missing ; at x = new int[x] - is that a typo?

int main()
{
    int* x = NULL;
    int n, numbers;
    std::cin >> n;
    x = new int[n];
    for (int i=0;i<n;i++){
        std::cin >> x[i];
        numbers = x[i];
    }

delete [] x;

Upvotes: 2

JorenHeit
JorenHeit

Reputation: 3997

Why not just create the array dynamically? This way, the user won't have to type in the number of elements in advance:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> vec;
    int x;
    while (cin >> x)
        vec.push_back(x);

    for (int y: vec)
        cout << y << ' ';
    cout << '\n';
}

The cout statements are just to illustrate that everything worked.

Upvotes: 1

Related Questions