Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to create a program that justifies the contents of the file. so far my code is this:
XML
#include <iostream>
#include <vector>
#include<string>
#include <fstream>
using namespace std;

int main()
{
int column=25;
vector<string>paragraph;
vector<string>words;
string str;

  ifstream fin("myFile.txt");

  do{
    str="";
    getline(fin,str);
    paragraph.push_back(str);
  }while(str!="");
fin.close();
words.push_back(paragraph.at(0));
for(int i=0;i<words.size();i++){
    str=words.at(i);
}
for(int i=0;i<column;i++){
    cout<<str.at(i);
    sum+=sum;

}

I put all the contents of the file in the vector paragraph. and each line on vector words. Ihave to display 25 characters on each line. what should be steps after this? Thank you for your help
Posted

1 solution

yikes .. talk about water long under the bridge .. hmmm

iirc, you need to do something like (and this is very rough)

a) set count of characters in sentence to 0
b) loop through the list of words
c) if word length + count of characters in sentence > 25, [output sentence]
d) add word to words for this sentence list
e) add word length to count of characters in sentence
f) end loop
g) output any remaining word left in words list

[output sentence]
simplistically, you :-
calculate the number of extra spaces you need ie 25 - characters in sentence
loop through the words for this sentence list
output the words separated by spaces and 'extra' spaces
clear the words for this sentence list
set the count of characters in sentence to 0
end loop

The trouble is "output the words separated by spaces and 'extra' spaces" - you need to balance where the spaces go, so you don't end up with all on the left or right side of the sentence
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900