0

I am working on a program where a user keeps entering numbers, which are saved into an array, when the array is full I am trying to copy the original array into a new one one and continue to fill that array however I cannot get it to work at all. Here is my code so far

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main()
{
    int size;
        cout << "Please enter how many numbers you want to enter: ";
    cin >> size; 
    double *array = new double*[size];
    cout << "Please enter your numbers: ";
    for(int i = 0; i < size; i++) {
        cin >> array[i];
        if(i == size-1) {
            int newSize = 2*size;
            double *arrayb = new double*[newSize];
            for(int i = 0;i<size;i++) {
                arrayb[i] = array[i];
            }
            delete [] array;
            array = arrayb;
            size = newSize;
        }
    }   
}
cggreeme
  • 21
  • 2

3 Answers3

1

If you don't know the maximum size of your collection before execution you need to avoid array. Like TartanLlama says, you can use an std::vector. Vector allows you to add items as much as you want. But there is plenty of container with different access method. See this link to have a first view of how choose your container : In which scenario do I use a particular STL container?

Community
  • 1
  • 1
baddger964
  • 1,199
  • 9
  • 18
  • You should choose a suitable container. http://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container – alirakiyan Nov 08 '16 at 11:22
0

I can get it to compile by making the double pointers point at doubles, instead of arrays of double pointers

int size;
cout << "Please enter how many numbers you want to enter: ";
cin >> size; 
double *array = new double[size];
//                        ^--- not pointers to doubles
cout << "Please enter your numbers: ";
for(int i = 0; i < size; i++) {
    cin >> array[i];
    if(i == size-1) {
        int newSize = 2*size;
        double *arrayb = new double[newSize];
        //                         ^--- not pointers
        for(int i = 0;i<size;i++) {
            arrayb[i] = array[i];
        }
        delete [] array;
        array = arrayb;
        size = newSize;
    }
} 

You have allocated enough memory in the first new for the data. BUT whn you get to one before the end of the current value of size you double the space you allocate. And make size = newSize. The outer for loop will never end, unless an exception gets thrown, for example a bad::alloc which is bound to happen eventually.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

If you read the compilation errors, you'll see the problem:

g++ -std=c++17 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds -O2 -Weffc++      14226370.cpp    -o 14226370
14226370.cpp: In function ‘int main()’:
14226370.cpp:11:37: error: cannot convert ‘double**’ to ‘double*’ in initialization
     double *array = new double*[size];
                                     ^
14226370.cpp:17:49: error: cannot convert ‘double**’ to ‘double*’ in initialization
             double *arrayb = new double*[newSize];

You are creating an array of pointers to double and attempting to initialise a single pointer to double, on both cases.

Looking at the way you use these, you probably meant to write new double[size] and new double[newSize].

Toby Speight
  • 27,591
  • 48
  • 66
  • 103