What the fuck is this shit!?! (having a problem with an array in c++)
8 replies, posted
...
I had same problem awhile ago
you need to create a pointer, and then assign the array to that
so i think it would be
int *filearray = new int[filesize];
You can't make static arrays of variable sizes.
[url]http://www.cplusplus.com/doc/tutorial/dynamic/[/url]
You can't use a variable to size an array like that. Some compilers allow it as a non-standard extension, but evidently Microsoft's doesn't. The number in the brackets for defining the size of an array has to be something that's known at compile time.
This isn't a good way to read all the numbers from the file anyway, though, because you have to go through the file twice: once just to count the lines, and then again to actually do something with them. (Right now you're not doing anything to reset the file pointer -- you read the whole file through to the end, then create the array, then try to read more from the file even though you're already at the end.)
A better approach would be to use a container, such as std::vector or std::list, to hold the numbers as you read them from the file. You don't need to know the total number of lines in advance because these containers grow automatically as you add things to them. You can go through the file just once: read a line, extract the number or whatever from it, add it to the end of the list, then loop around and read the next line.
ps dont forget t delete it after, to not have a memory leak
It won't work because it's fuckin shit.
You don't need the explicit "return 0" if that was meant as a correction; it's implicitly added at the end of main if you leave out a return statement there.
(There's also no need to use the two-argument version of main if you don't actually plan to use the arguments.)
Sorry, you need to Log In to post a reply to this thread.