Need help with dynamic string array

What I'm trying to do is fairly simple. I need to prompt the user for the number of words in a sentence, have them write a sentence, and then store it into an dynamic string array as large as the number of words.

The array stops storing the sentence when the same word is entered twice in a row, and ignores the second word.

I can get it to work when the array is static (and I don't get user input for size), but it brings up my debugger every time I try it with a dynamic array. It will compile, but not run. Any thoughts? Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main () {

	int words;
	
	cout << "Enter a sentence length: ";
	cin >> words;

	string* sentence = (new(nothrow) string[words]);//Declare dynamic string array
	if(sentence==NULL){
	        cout << "Cannot allocate.";
		exit(1);
	}
	
	cout << "Enter a phrase: ";
	for (int i=0; i<words; i++){
		cin>>sentence[i];//Read each input into an array
		if (sentence[i]==sentence[i-1]){//Check for the same word 
			sentence[i]="";
			break;
		}
	}
	
	cout << "Array results: ";//Printing the array for error checking
	for (int i=0; i<words; i++){
		cout << "[" <<sentence[i] << "]";
	}
	
	
	delete[] sentence;
	return 0;
}
Nevermind, I figured it out. It was my array bounds on line 17. Must have been a fluke that it worked with a static array.
Topic archived. No new replies allowed.