[Solved] how to store sentences (words) wit h spaces in an single array in cpp dynamically [closed]


#include <iostream>

int main()
{
  // 3 strings of 100 char max (+1 nullchar)
  char key[3][101];

  int i = 0;

  std::cout << "Enter the Keyword      :\n";

  while(i < 3) {
    std::cin.getline(key[i],100);
    i++;
  }

  for(i = 0; i < 3; ++i) {
    std::cout << key[i] << std::endl;
  }

  return 0;
}

Alternative anwser using C++ vector and string

#include <iostream>
#include <string>
#include <vector>

int main()
{
  // array of strings
  std::vector<std::string> keyw_list;

  // string for temporary operations
  std::string keyw;

  std::cout << "Enter the Keyword      :\n";

  while(key_list.size() < 3) {
    std::cin >> keyw;
    keyw_list.push_back(keyw);
  }

  for(size_t i = 0; i < keyw_list.size(); ++i) {
    std::cout << keyw_list[i] << std::endl;
  }

  return 0;
}

solved how to store sentences (words) wit h spaces in an single array in cpp dynamically [closed]