c++11 to c++98 conversion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
#include <sstream>
#include <list>
#include<vector>
using namespace std;
int width;
int pagewidth;
typedef list<string> WordList;
WordList splitTextIntoWords( const string &text )
{
WordList words;
istringstream in(text);
copy(istream_iterator<string>(in),
istream_iterator<string>(),
back_inserter(words));
return words;
}
void justifyLine( string line )
{
size_t pos = line.find_first_of(' ');
if (pos != string::npos) {
while (line.size() < pagewidth) {
pos = line.find_first_not_of(' ', pos);
line.insert(pos, " ");
pos = line.find_first_of(' ', pos+1);
if (pos == string::npos) {
pos = line.find_first_of(' ');
}
}
}
//cout<<pagewidth<<endl;
cout <<"|"<< line <<"|"<< endl;
//cout << line << endl;
}
void justifyText( const string &text )
{
WordList words = splitTextIntoWords(text);
string line;
for (string& word : words) {
if (line.size() + word.size() + 1 > pagewidth) { // next word doesn't fit into the line.
justifyLine(line);
line.clear();
line = word;
} else {
if (!line.empty()) {
line.append(" ");
}
line.append(word);
}
}
cout<<"|";
for(int i=0;i<pagewidth;i++){
cout<<"-";
}
cout<<"|"<<endl;
}
int main()
{
string input;
int width;
int num=0;
int max;
int newmax;
int range=0;
string addline;
while(getline(cin,input)){
addline=addline+input+" ";
if (input==" ")
{
for(size_t i=0;i<addline.length();i++){
if(addline[i]==' '){
max=i;
if(max>num){
newmax=max-num;
num=max;
if(newmax>range){
range=newmax;
}
}
}
}
cout<<"enter the text width"<<endl;
cin>>width;
do {
pagewidth=width;
if(pagewidth>range){
cout<<"|";
for(int i=0;i<pagewidth;i++){
cout<<"-";
}
cout<<"|"<<endl;
justifyText(addline);
cout<<"enter the text width"<<endl;
cin>>width;
}
else if(pagewidth>0 && pagewidth<=range)
{
cout<<"you should enter the page width greater than "<<range<<endl;
cout<<"enter the text width"<<endl;
cin>>width;
}
} while(width!=0);
break;
}
}
return 0;
}
|
the functions used in the code some of it are in c++ 11 format.
(ranged base for loop) help me fix those please
Last edited on
he uses mac and used some c++11 feature that i cant use (windows, eclipse ide) |
1. What C++ compiler (and which version of it) does your IDE use?
2. What errors does your compiler give for this code?
Topic archived. No new replies allowed.