I'm just starting to learn C ++, so excuse me for this simple question. What I'm doing is reading numbers from a file, and then trying to add them to an array. My problem is how to increase the size of the array? For example, I thought I could just do:
#include <iostream>
using namespace std;
int main() {
double *x;
x = new double[1];
x[0]=5;
x = new double[1];
x[1]=6;
cout << x[0] << "," << x[1] << endl;
return 0;
}
But this, obviously, just overwrites the value 5, which I originally set to x [0], and therefore gives 0.6. How would I make it give out 5.6?
Please understand that for the example that I included, I did not want to clutter it up by reading the code from the file or the code to get numbers from the user. In the actual application, I won’t know how big the array I need at compile time, so please don’t tell me to just create an array with two elements and set them to 5 and 6 respectively.
Thank you for your help.
source
share