Hello all, just hoping for a bit of understanding of new[]. I'm writing an insertion sort program for school (of course) and I've got most of it done; I have the program asking for a filename, and asking for file "size" (or how many numbers are in the file), and it successfully sorts. The problem I'm having is that I can't seem to figure out how to create a dynamic array for the file "size" when i don't know how big i need it to be. Once created, the size of the array won't change, so i won't need to worry about re-initializing or changing the array any.

//Got to read input file. Lab1.2 works on getting "any" input file to work.
#include <iostream>
#include <fstream>
#include <cstring>

int main()
using namespace std; 
int key, length, count, i, j;
length = 35;

char filename[50];
fstream fin;
cout << "Enter a filename to open: " << endl;
cin >> filename;

cout << "Enter number of integers to sort:" <<endl;
cin >> length;

char A[length];
for (count=0; count<length; count++)
    fin >> A[count];

Here is the part that is troubling me. I can create the dynamic array up above using int *A = new int, but it needs a size, so *A = "a number" won't exactly work, and I'd still need something for the "for" loop as a maximum value.
I'm using "length" for the size of the array so i can feed the information from the named file to the array "A". I can't omit the size either way, but I'm stuck "in the box" of thinking on how to do it.

cout << "String Length: " << strlen(A) << endl;

for (j=2; j<length; j++)
    key = A[j];
    while (A[i]>key && i>=0)
ofstream fout;
cout << "Sorted List: ";

for (i=0;i<length; i++)
cout << A[i];
fout << A[i];
return 0;         

I've been searching google, and I've heard of people recommending using vectors, malloc, and a few other things instead of new and delete, but since i'm only initializing once, i'd figure it'd be easy, eh?

oh, input from the file is in the form of "5123948021378041" so you just input the txt file name and then how many numbers you have in the file.
Anyways, any help or suggestions would be much much appreciated!

An example might help:

#include <iostream>
#include <cstdlib>

int main()
    std::size_t sz;

    std::cout << "Enter a size: ";

    if (std::cin >> sz)
        int* p = new int[sz];

        for (int x = 0; x < sz; ++x) p[x] = rand();
        for (int x = 0; x < sz; ++x) std::cout << p[x] << '\n';

        delete[] p;

Thanks for the feedback. I'm not sure its 100% what I'm looking for though since I'm still entering a size manually, unless i'm looking at your code wrong ^^;; I'm looking at making it automatically size after I enter a file to open. (can't use strlen or sizeof, since they just use the literal characters of "filename").

I think what I need to do is get rid of the "for" statement, and declare my "length" as the "new int" and perhaps use a "While" loop that runs as long as (cin != EOF) and then input fin >> A[length]....

I'm gonna try it after a bit (just getting my midterm to do tonight), again, any direction would be awesome.

Instead of using an array, why not use vector's
vector<char> A;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.