I'm still a bit of a newbie and have what is probably a minor issue, but for some reason I can't wrap my mind around it.
I have a program that does a whole bunch of stuff to text from an input file. The input looks like this:

MArch    9,    1971.  SPace the finAL      fronitIER!
    "TheSe aRe      the voyAges     of the    staRship

With the current result looking like:

March    9,    1971.  Space the final      fronitier!
        "These are      the voyages     of the    starship

But what I need to do is get rid of all of the spaces and format it so....

March 9, 1971.  Space the final fronitier!
"These are the voyages of the starship

If I read in the string one word at a time, I can easily concatenate the ouput , ( cout <<text+' ')
but the way that I read in the input, I used get(ch), which isn't playing nice for me so far.
So how do I concatenate the output?

Any help would be greatly appreciated and here is the code for this segment of my program...

    if (ch=='"')
        cout << (char)ch;
        cout << (char)toupper(ch);
      if( new_sentence )
    cout << (char)toupper(ch);
    cout << (char)tolower(ch);
      new_sentence = false;
      if( ch == '.' ||  ch == '!' ||  ch == '?') 
          new_sentence = true;
      cout << (char)ch;

Thanks in advance!

It really would be best to use word granularity rather than character granularity. Collect all of the words in a list and then you can use a simply greedy word wrapping algorithm to display them in a bounded area. For example:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <utility>
#include <vector>

using namespace std;

namespace {
    const string::size_type line_width = 40;

int main()
    ifstream in("test.txt");

    if (in) {
        vector<string> words;
        string line;

        // Build the word list
        while (getline(in, line)) {
            auto x = vector<string>(
                sregex_token_iterator(line.begin(), line.end(), regex("\\s+"), -1), 

            x.erase(remove_if(x.begin(), x.end(), [](const string& s) { return s.empty(); }), x.end());

            for (auto& word : x) {

        // Display each wrapped line
        auto remaining = line_width;

        cout << string(line_width, '#') << '\n';

        for (const auto& word : words) {
            auto width = word.size() + 1;
            bool wrap = width > remaining;

            cout << (wrap ? "\n" : "") << word << ' ';
            remaining = (wrap ? line_width : remaining) - width;

        cout << '\n' << string(line_width, '#') << '\n';

How you collect the words is up to you and generally straightforward, so I did something concise. The important part is the word wrapping algorithm.

This question has already been answered. Start a new discussion instead.