0

I know variable length arrays are not allowed in c++. My code is currently as follows:

int main(){
  int t;
  cin >> t;
  double h[t];
}

How can I create an array with length t then?

Edit: the assignment only allows char, bool, int and double. Vector isn't allowed. When I tried to compile it, it says ISO C++ forbids variable length array 'h'.

12
  • 7
    Use std::vector<double> h(t);. (And, don't forget to #include <vector> before.) Commented Mar 11, 2021 at 17:07
  • 2
    I'm sure searching could've revealed this. Commented Mar 11, 2021 at 17:08
  • 3
    If you can't use vector, then search about new[]. Commented Mar 11, 2021 at 17:10
  • 4
    An alternative would be to use double *h = new double[t];. (A proper program should delete[] h; after you don't need h anymore.) Commented Mar 11, 2021 at 17:10
  • 5
    ...and you may send your teacher the following link: Kate Gregory: Stop Teaching C... ;-) Commented Mar 11, 2021 at 17:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.