I am trying to load in a list of words into my program using a dynamic array and the string class....for some reason, all the tutorials are using char which i have no idea how to adapt to my code....

string *strWord;
	int Position = 0; 
	string strHolding;
	//for loading files
	ifstream fp_in;  // declarations of streams fp_in and fp_out
	ofstream fp_out;
	fp_in.open("WordList.WORD", ios::in);    // open the streams
	do {
		strWord = new string[Position];
		fp_in >> strWord[Position];
		Position =+ 1;
	}while (Position < 47);

	fp_in.close();   // close the streams
7 Years
Discussion Span
Last Post by mvmalderen

just some extra information....
i am able to run the program but when it gets to the string declaration it crashes and i get an access violation


Why don't you put some couts and check the values of 'Position' ? and btw you are leaking a lot of memory.


and btw you are leaking a lot of memory.

bryansworld, welcome to the world of C++ !

You're indeed leaking a lot of memory:
> What are you using variable 'strHolding' for ?
> Not very clever to insert the instruction strWord = new string[Position]; inside a loop as the program will always assign new memory and the old will still be allocated, but you don't have access to it anymore because you aren't having any pointer which is pointing to that memory ...

After you've rewritten your program:
> You shouldn't forget to release the allocated memory when it isn't needed anymore ...


BTW, Position =+ 1; is always setting variable Position to 1 ...

I think you meant Position += 1; (if your intention was to increment variable Position each time with one)

As a result your program will throw itself into an infinite loop (*) where it's always allocating more and more and more ... memory which explains the crash ...

(*): You're always checking whether Position < 47 , but this is always true as Position =+ 1; is everytime setting Position's value to 1, and 1 is always lower than 47 I hope this explains your mistake(s) !

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.