4

so I'm taking in input by

string input,word;
while(getline(cin, input)) {
 stringstream ss; 
 ss<<input
 while(ss >> word)
 ...

and printing the stuff I want by cout <<word<<" ";

However, when I do this it creates an extra whitespace at the end of each line and I don't want that. If I don't put that whitespace there there will be no spaces between words when I print them out. Is there an easier way to cout the words I want so that they are automatically spaced? What are some of the things I could do to prevent the extra whitespace at the end of each line.

Gui Montag
  • 49
  • 1
  • 3
  • Similar to this: https://stackoverflow.com/questions/35858896/c-compare-and-replace-last-character-of-stringstream/35859132#35859132 and this: https://stackoverflow.com/questions/36137997/convert-vectorunsigned-char-1-2-3-into-string-1-2-3-as-digits/36138229#36138229 – Galik Jun 02 '16 at 20:40
  • 2
    Right now you're putting the white-space after the word. You could fix this by printing the first word outside of the while loop, and then printing the whitespace *before* the each word. Handling the first word separately will avoid a leading whitespace. – aquirdturtle Jun 02 '16 at 20:41
  • Won't std::getline work for this? – Random Davis Jun 02 '16 at 20:53
  • @RandomDavis: the issue is with output, not input. – Remy Lebeau Jun 02 '16 at 23:53

3 Answers3

5

Test the first word extraction outside the loop, then continue the loop with leading spaces:

std::string input;
while(getline(std::cin, input))
{
    std::istringstream ss(input);
    std::string word;
    if (ss >> word)
    {
        std::cout << word;
        while (ss >> word)
            std::cout << ' ' << word;
        std::cout << '\n';
    }
}

Note: newline added for line completion. Use or delete at your discretion.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • This method only works for some cases due to the nature of what I want to do. Let's say I need to create a new line only if the input I read in is ";" if I'm given input that consists of multiple lines with only 1 word that are not ";" I need to print them in one line with a space between each word. If I do this, all those words will have no spaces in between. – Gui Montag Jun 03 '16 at 01:40
  • @GuiMontag This method works for *exactly* the problem your question described. If you need something else, the question should reflect that. In your comment "Let's say I need.... only if...", the very presence of "only if" constitute additional boolean checks and actions therein. Those checks simply need to be translated into code. – WhozCraig Jun 03 '16 at 09:58
2

Flip the problem around. Instead of appending a space, figure out when you should prepend a space.

The way I do this is to initialize a prefix delimiter to an empty value, then set it to something valid after the first element. In your case, it would be like this:

const char *delimiter= "";
while(ss >> word)
{
    ss >> delimiter >> word;
    delimiter= " ";
}
MSN
  • 53,214
  • 7
  • 75
  • 105
0

A trick - which still feels a bit frustrating - is adding a boolean variable in the mix:

bool space = false;
while (ss >> word) {
    if (space) {
         cout << " ";
    } else {
         space = true;
    }

    cout << word;
}
coyotte508
  • 9,175
  • 6
  • 44
  • 63
  • Sadly the second example doesn't work. It exhibits the same problem. – Galik Jun 02 '16 at 20:46
  • I tried the first method and I got the same problem. I'm not sure why though. Shouldn't the loop stop once input stops? – Gui Montag Jun 02 '16 at 21:06
  • @Galik, thanks removed. @Gui Montag you should put this code as replacement of your `while(ss >> word)` and there should be no difference in reading the input... – coyotte508 Jun 02 '16 at 23:34