c++ - Width constraint of the output lines to the console -


class prints string console. how make width of output lines equal characterwidth = 40, ie after 40 characters transferred new line?

#include <string> #include <iostream>  class stringprocessing {   public:       stringprocessing() : characterwidth(40),                      texttobeformatted("null") {}        inline void stringprocessing::initstring() {           texttobeformatted =               "text    text   text text   text text"               "text text  text    text text text"               "text         text    text   text text"               "text  text    text    text text text"               "text       text text   text text text";       }        inline void displaystring()         { std::cout << texttobeformatted << std::endl; }   private:           int characterwidth;       std::string texttobeformatted; }; 

i have idea here words in console cut off, need transferred next line , perform width alignment

inline void displaystring()       {          const std::string& s = texttobeformatted;          (int = 0; < s.length() / 40 + (bool)(s.length() % 40); ++i)          {             std::cout << std::left                       << std::setfill(' ')                       << std::setw(40)                       << s.substr(i * 40, 40)                       << std::endl;          }       } 

here answer suitable me

#include <string> #include <iostream> #include <iomanip>  class stringprocessing { public:     stringprocessing() : characterwidth(40),         texttobeformatted("null") {}      inline void initstring() {         texttobeformatted = "text    text   text text   text text"             "text text  text    text text text"             "text         text    text   text text text"             "text  text    text    text text text"             "text       text text   text text text";     }      inline void displaystring()     {         const std::string& s = texttobeformatted;         const int& width = characterwidth;         (int current = 0; current < s.length();)         {             if (s.length() < width)             {                 output(s);                 break;             }             if (s.length() - current < width)             {                 output(s.substr(current));                 break;             }             std::string substr = s.substr(current, width);             current += width;             size_t space = substr.rfind(' ');             if (space != std::string::npos && (substr[width - 1] != ' ' &&                 (s.length() > current && s[current] != ' ')))             {                 current -= width - space - 1;                 substr = substr.substr(0, space + 1);             }             output(substr);         }     } private:     inline void output(const std::string& s)     {         std::cout << setfill(' ') << std::right << std::setw(characterwidth) << s << std::endl;     }     int characterwidth;     std::string texttobeformatted; }; 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -