Can't concatenate more than one char into a string at a time?

Hey all,

So I've got this basic question. I have these 5 chars with a single value assigned to each. I would like to take those 5 chars and stick them into an empty string variable. Now I'm able to do it one by one, but, it would be greatly appreciated if someone could inform me of a way to do them all at once.

Here is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

using namespace std;

int main()
{
   char char1 = 'g';
   char char2 = 'h';
   char char3 = 'a';
   char char4 = 'd';
   char char5 = '!';

   string str1;

   str1.clear();

   str1  = char1;
   str1 += char2;
   str1 += char3;
   str1 += char4;
   str1 += char5;

   cout << str1 << endl;

   return 0;
}


Here's the output:

ghad!
Press any key to continue . . .


I can get the same thing done using:

1
2
3
str1.append(1, char1);
str1.append(1, char2);
etc...


But again, that only does it one at a time... =/

Thank you in advance!
Try this:
str1 = string() + char1 + char2 + char3 + char4 + char5;
string() is an empty string object, this changes the left operand of the first + so that it will be returning a string which will change the second + and so on
Genius.

Thanks a lot Bazzy! Works perfectly.
Topic archived. No new replies allowed.