Search code examples
c++dynamic-memory-allocation

Dynamically allocated array C++ reads a sentence and prints the words


On the input we get a sentence which we read until EOF. We need to add single words to dynamic array and then write them one on each line.

Input: Hello this, is an example.

Output:

Hello

this,

is

an

example

I have the following code and I can't figure out why it doesn't even add anything to the array.

#include <iostream>
#include <string>

using namespace std;

void addToArray(string newWord, string myArray[], int& arrayLength)
{
  string * tempArray = new string[arrayLength + 1];
  tempArray[arrayLength] = newWord;

  for (int i = 0; i < arrayLength; ++i)
  {
    myArray[i] = tempArray[i];
  }

  arrayLength++;
  myArray = tempArray;

  delete [] tempArray;
}

int main()
{
  string * arrayOfWOrds = new string[1000];
  int arrayLength = 0;
  string temp;

  while (getline(cin, temp))
  {
    cout << temp << endl;
    addToArray(temp, arrayOfWOrds, arrayLength);
  }

  cout << "Array" << endl;
  for (int i = 0; i < arrayLength; ++i)
  {
    cout << arrayOfWOrds[i] << endl;
  }

}

Solution

  • I see several issues here. To begin with, you set arrayLength = 0, so it's not going to iterate over the whole array if you have stuff already in it. If you don't have anything in it, there's no point in making it start off with 1000 items. Also, while (getline(cin,temp)) is an infinite loop, so it will never end and actually print the array. If you want to print the array after each addition, you need to move it into the while loop. There's no real reason to cout the number the user types either; they can already see the line they just typed.

    More importantly, there are real issues with the dynamic allocation. You've created a static array (string * arrayOfWOrds = new string[1000];), then you're giving that to the function which makes a new array one item larger, sets the last item in that array to the new value, then iterates over the entire new array and duplicates the values to the old array. Basically, you're just inserting items into the static array at that point, and what you're inserting is a bunch of nothing (because the new array only has one item in it, and it's at arrayLength+1 which is outside the bounds of the original array).

    You need to delete the old array, not the new array, which actually should be thrown on the heap and returned.

    Basically, it should look more like this:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string* addToArray(string newWord, string myArray[], int& arrayLength)
    {
    
      string * returnArray = new string[arrayLength + 1];
      returnArray[arrayLength] = newWord;
    
      for (int i = 0; i < arrayLength; ++i)
      {
        returnArray[i] = myArray[i];
      }
      arrayLength++;
      delete [] myArray;
    
      return returnArray;
    }
    
    int main()
    {
      const int startSize = 0;
      string * arrayOfWords = new string[1];
      int arrayLength = startSize;
      string temp;
    
      cout << "Input: ";
      getline(cin, temp);
    
      string word = "";
      for (char c : temp){
        if (c == ' '){
          arrayOfWords = addToArray(word, arrayOfWords, arrayLength);
          word = "";
        } else word.push_back(c);
      }
      arrayOfWords = addToArray(word, arrayOfWords, arrayLength); // Don't forget the last word
      for (int i = 0; i < arrayLength; ++i)
      {
        cout << arrayOfWords[i] << endl;
      }
    }