Advertisement
avr39ripe

cppStrContainerDyn2D

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