Advertisement
avr39-ripe

strArr2DContainer

Apr 18th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 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);
  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.         std::cout << "\nAdd another line? [Y/y to add]";
  25.         std::cin >> addMore;
  26.         std::cin.ignore(32768, '\n');
  27.     }
  28.  
  29.     std::cout << "Here is yor lines \n";
  30.  
  31.     for (textPos = 0; text[textPos]; ++textPos)
  32.     {
  33.         std::cout << text[textPos] << '\n';
  34.     }
  35.  
  36.     std::cout << "Memory used to store this text: " << memoryUsed << '\n';
  37.  
  38.     for (textPos = 0; text[textPos]; ++textPos)
  39.     {
  40.         delete[] text[textPos];
  41.     }
  42.     delete[] text;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement