-2

I've been trying to write this code on concatenated strings:

#include<iostream>
#include<string>
using namespace std;

int main (){
    string line, total;
    //Read strings from the input//
    cout<<"Enter a String"<< endl;
    while(getline(cin,line)){
        cout<< "Enter another String"<< endl;
        if(!line.empty())
        total+=line;
        cout<<" Concatenated string is\t"<<total<< endl;
    }

    return 0;
}

However, my output has no space existing between the two appended strings. I also need a way to terminate the added strings.

OJFord
  • 10,522
  • 8
  • 64
  • 98
dirty_sanchez
  • 69
  • 1
  • 1
  • 12
  • string's operator+= does not implicitly add space. So you have to do it manually: `total += " " + line;`. – Creris Jan 31 '15 at 15:38
  • There are plenty of answers like here: http://stackoverflow.com/questions/662918/how-do-i-concatenate-multiple-c-strings-on-one-line. See comment above for a simpler solution – smagnan Jan 31 '15 at 15:38
  • What do you mean by 'terminating' the added strings? – nullptr Jan 31 '15 at 15:39
  • I meant if you run the code, each instance I prompt the user to enter a string, both the enter a string prompt and the concatenated string output appear on the screen together. I was hoping that the output displays enter a string first and then the next line would display enter another string etc thus then the following line would display the concatenated string if I only choose to enter only two strings. Thanks – dirty_sanchez Feb 01 '15 at 00:39

3 Answers3

1

Operator += does not add spaces for you. You would need to add space explicitly, something like total += " " + line;

nullptr
  • 11,008
  • 1
  • 23
  • 18
1

+= doesn’t automatically adds space between two strings. You need to manually add it.

Something like this:

total += " " + line;

Also, this article provides explanation with example about += operator. http://www.cplusplus.com/reference/string/string/operator+=/

sam
  • 2,033
  • 2
  • 10
  • 13
0

"However, my output has no space existing between the two appended strings."

To put spaces between your concatenated strings, just add them

 total += ' ' + line;
       // ^^^

"I also needed a way to terminate the added strings."

To end the loop you could for instance have an else branch on the if(!line.empty()) test with a break; statement.
When an empty string was input, the loop and the program ends.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I had abrupt exiting of the windows C++ console when I included the break statement. so I tried the if else statement without the break statement, it works without the break. However when I enter an empty string upon a prompt. The output displays the enter the string prompt, with the appended string output. I was under the impression that upon entering the empty string only the appended string would be printed not with the prompt enter a string. – dirty_sanchez Feb 01 '15 at 03:08
  • @dirty_sanchez _"I had abrupt exiting of the windows C++ console when I included the break statement"_ You can run your program in debug mode and set a breakpoint at the last brace `}` in `main()`. That will keep the console open. Alternatively you can put a `getch();` statement after your loop. – πάντα ῥεῖ Feb 01 '15 at 05:15