Two or more string literals separated only by whitespace are automatically concatenated:
cout << "Hello,"
"world"
"!";
A string is a sequence of characters, and can be initialized by a string literal, by another string. However, a string cannot be initialized by a char. "Every string starts out with a value."
#include <string>
string s1; // the empty or null string
string s2 = ""; // also the empty string
string s3 = 'a'; // error
string s4 = "ab";
string s5 = s4; // copy of "ab"
s1 = s5 + 'c'; // "abc"
s2 = 'c' + "def"; // error: + does not work with string literals
s1 = "world";
cout << "Hello,"
s1; //error: automatic concatenation does not work with string vars
The >> operator reads a whitespace-terminated word to its string,
expanding the string as needed to hold the word. Initial whitespace is skipped.
The getline() function reads a line terminated by newline ('\n') to its string,
expanding the string as needed. The newline is removed from the input stream
but is not entered into the string.
Here's how to use getline() with an eof-controlled while loop:
// getline with strings
#include <iostream>
#include <string>
using namespace std;
int main () {
string str;
//prompt and read
cout << "Please enter full name: ";
//driver loop
while(getline(cin,str)){
cout << "Thank you, " << str << endl;
//prompt and read
cout << "Please enter full name: ";
}
}