So I am working on a problem and have a bit of an issue that I can't seem to figure out and would like some guidance.

I have string input coming into my program via fstream. Its a bunch of sentences where I have to cout it in such a way where each line can be no more than 40 characters. I can't hyphenate words so if a word is going to go over, the code must move it to the next line. The entirety of the program does a bunch of other stuff, but here is my void function where I have my cout statement. The function capitalizes each word and is not related to my issue.

void format(string& word) 
{ 

  word[0] = toupper(word[0]);

  for (int i=1; i<word.length(); i++)
      word[i] = tolower(word[i]);

  cout << word+' ';

}

I have tried different types of for loops where I put an if statement that if it hits word[i] hits 40 to do a "cout << endl;" but that didn't work. I played around with setw but that was wrong, (as I should have known).

Any help to point me in the right direction will be greatly appreciated.

What all is in word? is it just one word or a sentence. I ask because I dont know of a word that is 40 letters long outside of chemical compounds. How do you want to split the word? If the word puts you over 40 charecters do you split it or do you put the whole word on a new line.

Accept input as a single string of size n. Start at index i and look at index i + 39, call it j. if input[j] is whitespace, then output all chars from i to j. If j isn't whitespace then go backward from j until you find a whitespace char and output all chars from i to j. In either case go to a new line after you output the current one. Assign j + 1 to i and repeat as many times as needed. Remember to be careful to maintain valid indexes as you go.

AH, my bad, let me elaborate some more.

Yes, if the word puts me over I need the whole word on a new line.
The input is a bunch of sentences like the following

apRil       8,  2013.
 tHE follOWing are excerpts           from
ChaPter 3 of the       textbook!
"the VAriABLES Cin and Cout are already
           defined and      assoCIATED 
with     the      STANDard INPUT/OUTPUt
  devices."    dOES   iT   WOrk?

Here is the solution to my problem, although I made a separate post about an output issue in terms of concatenation....

input.get(ch);
    if (ch=='"')
    {
        new_sentence=true;
        cout << (char)ch;
    }
    else
    {
        new_sentence=false;
        cout << (char)toupper(ch);
    }
    while(input.get(ch))
    {
   if(isalpha(ch))
   {
      if( new_sentence )
    cout << (char)toupper(ch);
      else 
    cout << (char)tolower(ch);
      new_sentence = false;
   }
   else
   {
      if( ch == '.' ||  ch == '!' ||  ch == '?') 
          new_sentence = true;
      cout << (char)ch;
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.