Format Output to a maximum character width

I have to write some code where I must format the output of a bunch of sentences in no more than 40 characters per line. I can't use hyphens to truncate words. The program must know if it's not going to fit that it must start a new line.

The input is coming from a file, and input into the program via ifstream.

I haven't tried anything yet, but wanted some suggestions so I can get going because I haven't, as of yet, found any function that can help me accomplish this.
The code I do have does a whole bunch of unrelated stuff to the input and I didn't want to fill up the post with jibberish.

I'm thinking that since I'm using strings I can use the length or size functions, but I don't see just yet how best to use them in this scenario.

Thanks in advance.
closed account (3qX21hU5)
I would just read it all into a string then walk through that string printing each character until you hit 40 characters then start a new line and continue walking through the string printing 40 character, ect. until you hit the end of the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string myString("how are you doing?");
    int newLineChecker = 5;

    for (auto i = 0; i != myString.size(); ++i)
    {
        if (i == newLineChecker)
        {
            newLineChecker += 5;
            cout << endl;
        }

        cout << myString[i];
    }
}


This code doesn't worry about if its breaking up a word so you will need to figure out something to figure out that part (Can't give you all the answers ;p), but it should give you something to start with.
Thanks for the reply and the help.
Topic archived. No new replies allowed.