***Less than or equal to 40 characters / line*** You should be getting the length of each word already. Before you output this word to the output file, you should add the length of the word to a variable holding the sum of characters in your current line. Don't forget to add in the length of your word plus 1 for the space after the word. sum = sum + length_of_word + 1; Again, BEFORE you output the word, you should then check this sum to see if it is greater than 40 characters. If it is greater than 40, output an endl (newline character) and THEN output your word plus the space after it. So, the word that causes your line to go over 40 characters should be output to the next line and not cause your line to be over 40. You also must take care of the sum variable when you start a new line. The sum needs to be reinitialized to the length of the next line you are starting. Remember, you output the word to the next line, so you must reinitialize the sum variable to the length of the word you just output to the next line (plus 1 for the space, of course). sum = length_of_word + 1;