0

I used a do while loop to ask user to enter integer as long as its not 0 or it will exit the program. I'm stuck on how to store every user input into the dynamically allocated array.

#include <iostream>
using namespace std;


int main() {
  int *A;
  A= new int();
  int n;
  do{
    cout<<"Enter integer: "<<endl;
    cin>>n;
    cout<< *A + n << endl;

    
  }while(n!=0);

  return 0;
}
10
  • 3
    use std::vector Commented Feb 1, 2022 at 1:31
  • @pm100 we haven't learned about vectors yet Commented Feb 1, 2022 at 1:33
  • Have you learnt how to use std::realloc? I am not recommending that you use that function, I am just asking, in order to try to guess how you are supposed to solve the problem. Commented Feb 1, 2022 at 1:37
  • @AndreasWenzel not yet Commented Feb 1, 2022 at 1:44
  • @mluong then what HAVE you learned so far? Commented Feb 1, 2022 at 1:44

1 Answer 1

2

You are allocating a single int, not an array of ints.

Also, even if you were allocating an array, a statement like *A + n does not add n to the array. It dereferences A to access the value of the 1st int in the array, and then adds n to that value.

Try something more like this instead:

#include <iostream>
using namespace std;

int main() {
  int *A = nullptr;
  int count = 0, n;

  do{
    cout << "Enter integer: " << endl;
    cin >> n;
    if (n == 0) break;
    
    int *newA = new int[count+1];
    for(int i = 0; i < count; ++i) newA[i] = A[i];
    newA[count] = n;
    delete[] A;
    A = newA;
    ++count;

    cout << A[count-1] << endl;
  }
  while (true);

  delete[] A;

  return 0;
}

Alternatively, try to avoid reallocating the array on every input, eg:

#include <iostream>
using namespace std;

int main() {
  int *A = new int[5];
  int count = 0, cap = 5, n;

  do{
    cout << "Enter integer: " << endl;
    cin >> n;
    if (n == 0) break;
    
    if (count == cap)
    {
        int newCap = cap * 1.5;
        int *newA = new int[newCap];
        for(int i = 0; i < count; ++i) newA[i] = A[i];
        delete[] A;
        A = newA;
        cap = newCap;
    }

    A[count] = n;
    ++count;

    cout << A[count-1] << endl;
  }
  while (true);

  delete[] A;

  return 0;
}

That being said, a better option is to use a std::vector, which will handle the dynamic memory for you, eg:

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

int main() {
  vector<int> A;
  int n;
  do{
    cout << "Enter integer: " << endl;
    cin >> n;
    if (n == 0) break;
    A.push_back(n);
    cout << A.back() << endl;
  }
  while (true);

  return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

and lets encourage not using using namespace std; from day one.

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.