storing bunch of words in a string,

how do i store 5 words in a single string? and output it as a single string? like for example,

jake
blake
hen
for
luke

then store this 5 words in same format as you can see into a single variable so that i can represent or call it in just one variable. thanks for anyone who will help me, i appreciate it.
Last edited on
Easiest option
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() 
{
  string words = "jake\nblake\nhen\nfor\nluke";

  cout << words << "\n\n";
 
  system("pause");
  return 0;
}

Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main() 
{
  string OneVar;

  OneVar += "jake";
  OneVar += "\n";
  OneVar += "blake";
  OneVar += "\n";
  OneVar += "hen";
  OneVar += "\n";
  OneVar += "for";
  OneVar += "\n";
  OneVar += "luke";

  cout << OneVar << "\n\n";
 
  system("pause");
  return 0;
}


Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() 
{
  ostringstream oss;

  oss << "jake" << "\n" << "blake" << "\n" << "hen" 
      << "\n" << "for" <<  "\n" << "luke";

  cout << oss.str() << "\n\n";
 
  system("pause");
  return 0;
}
okay thanks, but what if i would like those 5 words will come from the user, then the 5 words will be store in a single string variable in that format?
Then use the middle example from Thomas1965 above.
That is, make use of the += operator to concatenate the strings one at a time.

http://www.cplusplus.com/reference/string/string/operator+=/

Topic archived. No new replies allowed.