Creating an array of ints in main()

Could someone please tell me how exactly I'm supposed to "create an array of ints? I will be given a .txt file that I will have to read with an unknown amount of data, so I don't know exactly what size the array will be.
You'll have to use dynamic allocation. Look up new/delete on this site for more usage information/examples.
something like this?

int n;
int *array = new int[n];
delete []array;

No. Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
#include <ifstream>

int main()
{
    std::vector<int> array ;

    std::ifstream file ( "myfile" ) ;
    int n ;
    while( file >> n ) array.push_back(n) ;
   
    // array.size() ints have been read into array
   
    // use array
}

http://www.mochima.com/tutorials/vectors.html
ok, I sort of see what you're doing now. Now on your comment for array.size(), your basically finding the size of the array right. And nothing goes between the parentheses? And then I don't know what you mean for your comment //use array.
> array.size(), your basically finding the size of the array right.

Yes.

> And then I don't know what you mean for your comment //use array.

You wouldn't have read the numbers in the file into array unless you wanted to do something with those numbers. So use the array - do whatever it is that you are supposed to do with the numbers.

Read a tutorial on vectors: http://www.cprogramming.com/tutorial/stl/vector.html
Topic archived. No new replies allowed.