Language: C++

#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

string buf[101];

int main() {
  string input;
  vector<string> vs;
  int plen,cnum,width,cspace;
  while(cin>>plen, plen) {
    cin>>cnum>>width>>cspace;
    cin.ignore();
    vs.clear();
    while(true) {
      getline(cin, input);
      if(input == "?") break;
      vs.push_back(input);
    }

    vector<string> normalized;
    for(int i=0; i<vs.size(); ++i) {
      while(vs[i].length() > width) {
        normalized.push_back(vs[i].substr(0,width));
        vs[i] = vs[i].substr(width);
      }
      normalized.push_back(vs[i]);
    }

    for(int i=0; i<normalized.size(); ++i) {
      if(normalized[i].length() < width)
        normalized[i] += string(width-normalized[i].length(), '.');
    }

    if(normalized.size()%(plen*cnum) > 0) {
      for(int i=0; i<normalized.size()%(plen*cnum); ++i) {
        normalized.push_back(string(width, '.'));
      }
    }

    vector<vector<string> > pages;
    for(int i=0; i<normalized.size(); i += plen * cnum) {
      vector<string> op;
      for(int j=0; j<plen*cnum; ++j)
        op.push_back(normalized[i+j]);
      pages.push_back(op);
    }

    for(int i=0; i<pages.size(); ++i) {
      for(int j=0; j<100; ++j) buf[j] = "";
      for(int j=0; j<pages[i].size(); ++j) {
        buf[j%plen] += (j>=plen?".":"") + pages[i][j];
      }
      for(int i=0; i<plen; ++i) cout<<buf[i]<<endl;
      cout<<"#"<<endl;
    }
    cout<<"?"<<endl;
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:19:9: error: 'cin' was not declared in this scope
   19 |   while(cin>>plen, plen) {
      |         ^~~
a.cc:6:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    5 | #include <queue>
  +++ |+#include <iostream>
    6 | using namespace std;
a.cc:62:33: error: 'cout' was not declared in this scope
   62 |       for(int i=0; i<plen; ++i) cout<<buf[i]<<endl;
      |                                 ^~~~
a.cc:62:33: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:62:47: error: 'endl' was not declared in this scope
   62 |       for(int i=0; i<plen; ++i) cout<<buf[i]<<endl;
      |                                               ^~~~
a.cc:6:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    5 | #include <queue>
  +++ |+#include <ostream>
    6 | using namespace std;
a.cc:63:7: error: 'cout' was not declared in this scope
   63 |       cout<<"#"<<endl;
      |       ^~~~
a.cc:63:7: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:63:18: error: 'endl' was not declared in this scope
   63 |       cout<<"#"<<endl;
      |                  ^~~~
a.cc:63:18: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:65:5: error: 'cout' was not declared in this scope
   65 |     cout<<"?"<<endl;
      |     ^~~~
a.cc:65:5: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:65:16: error: 'endl' was not declared in this scope
   65 |     cout<<"?"<<endl;
      |                ^~~~
a.cc:65:16: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'