Advertisement
avr39ripe

strContainerDyn2DarrFIXED

Jan 14th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int maxLines{ 100 };
  6.     const int maxLen{ 200 };
  7.     char** text{ new char* [maxLines] {} };
  8.     char buf[maxLen]{};
  9.  
  10.     int textPos{ 0 };
  11.     char addMore{ 'Y' };
  12.     int memoryUsed{ 0 };
  13.  
  14.     while ((addMore == 'Y' or addMore == 'y') and textPos < maxLines)
  15.     {
  16.         std::cout << textPos << ". ";
  17.         std::cin.getline(buf, (maxLen-1));
  18.         int len = strlen(buf) + 1;
  19.         memoryUsed += len;
  20.         text[textPos] = new char[len];
  21.         strcpy_s(text[textPos], len, buf);
  22.         ++textPos;
  23.  
  24.         if (std::cin.fail()) // in case of error from std::cin.getline()
  25.         {
  26.             std::cin.clear();
  27.             std::cin.ignore(32768, '\n');
  28.         }
  29.         std::cout << "\nAdd another line? [Y/y to add]";
  30.         std::cin >> addMore;
  31.         std::cin.ignore(32768, '\n');
  32.     }
  33.  
  34.     std::cout << "Here is yor lines \n";
  35.  
  36.     for (textPos = 0; text[textPos]; ++textPos)
  37.     {
  38.         std::cout << text[textPos] << '\n';
  39.     }
  40.  
  41.     std::cout << "Memory used to store this text: " << memoryUsed << '\n';
  42.  
  43.     for (textPos = 0; text[textPos]; ++textPos)
  44.     {
  45.         delete[] text[textPos];
  46.     }
  47.     delete[] text;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement