Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- const int maxLines{ 100 };
- const int maxLen{ 5 };
- //char text[maxLines][maxLen]; // memory used = 20000 bytes
- char* text[maxLines]{}; // memory used sizeof(char*) * maxLines = 4 * 100 = 400 bytes
- //char* buf{ new char[maxLen] };
- char buf[maxLen];
- int memoryUsed{ 0 };
- char menu{ 'Y' };
- int textPos{};
- int strLength{};
- std::cout << "Enter text lines one by one, max 199 chars long:\n";
- do
- {
- std::cout << textPos << ": ";
- std::cin.getline(buf, maxLen - 1);
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cout << "Too long line!\n";
- //std::cin.ignore(32768, '\n');
- }
- strLength = strlen(buf) + 1;
- text[textPos] = new char[strLength];
- strcpy_s(text[textPos], strLength, buf);
- ++textPos;
- memoryUsed += strLength;
- std::cout << "Enter one more string? y/n\n";
- std::cin >> menu;
- std::cin.ignore(100, '\n');
- } while ((menu == 'Y' or menu == 'y') and textPos < maxLines);
- for (textPos = 0; textPos < maxLines and text[textPos]; ++textPos)
- {
- std::cout << textPos << " : " << text[textPos] << '\n';
- delete[] text[textPos];
- }
- std::cout << "Memory used " << memoryUsed << '\n';
- //delete[] buf;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement