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:
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;
}