0

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!

8
  • When using cin you need to press enter for each entry. For instance: 1 <enter> 2 <enter> , etc Commented Feb 6, 2014 at 19:24
  • 2
    @Pedrom That is incorrect. Commented Feb 6, 2014 at 19:24
  • 3
    What do you mean it's storing it as [123] instead of [1,2,3]? Commented Feb 6, 2014 at 19:25
  • @ZacHowland No, it is correct, you need to include a blank separator in order to cin separate the items. Enter key works well for me but an space would do the trick too Commented Feb 6, 2014 at 19:30
  • @Pedrom 'you need to include a blank separator in order to cin separate the items.' Aha! Was that being asked for?? Commented Feb 6, 2014 at 19:31

4 Answers 4

2

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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 << "]";

13 Comments

"C++'s vector takes care of memory allocation for you." Correct, which is why it is not necessary to ask for the size of the vector (n).
@JorenHeit Its not about the vector size but the amount of numbers the user wants to enter.
@FrançoisMoisan Why would you want to know that? That's what CTRL+D (EOF) is for, right?
@JorenHeit Sure, but its unclear if that is a requirement for OP's task (asking for count explicitly) or if he just did it to allocate the array.
@FrançoisMoisan As he was explicitly allocating the array, I assumed that that was his purpose. Anyway, even if he needs the count afterwards, it can be easily queried from std::vector's size() member. No need to bother the user with it.
|
2

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;
}

1 Comment

in the first for loop can we have the following code, "for (int i=0;i<n;i++) { std::cin >> x[i]; }''
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.