Class prints a string to the console. How to make the width of output lines is equal characterWidth = 40, ie after 40 characters was transferred to a new line?
#include <string>
#include <iostream>
class StringProcessing {
public:
StringProcessing() : characterWidth(40),
textToBeFormatted("NULL") {}
inline void StringProcessing::initString() {
textToBeFormatted =
"text some text some text some text some text some text"
"text some text some text some text some text some text"
"text some text some text some text some text"
"text some text some text some text some text some text"
"text some text some text some text some text some text";
}
inline void displayString()
{ std::cout << textToBeFormatted << std::endl; }
private:
int characterWidth;
std::string textToBeFormatted;
};
I have an idea but here the words in the console are cut off, so they need to be transferred to the next line and perform a width alignment
inline void displayString()
{
const std::string& s = textToBeFormatted;
for (int i = 0; i < s.length() / 40 + (bool)(s.length() % 40); ++i)
{
std::cout << std::left
<< std::setfill(' ')
<< std::setw(40)
<< s.substr(i * 40, 40)
<< std::endl;
}
}