1 2
|
result += 'a[i]';
result += 'b[i]';
|
should be:
1 2
|
result += a[i];
result += b[i];
|
Single quotes are used to express a character literal. So 'a' is the ASCII letter a and '3' is the ASCII character 3 (NOT the number 3). So the compiler doesn't understand
'a[i]'
because the quote makes it think you're entering a character literal but a[i] isn't a character a character literal.
Without the single quotes,
a[i]
is an expression that evaluates to the character at position i within the string a. That's what you want.
Pay attention to the text of the error messages. On my compiler (g++) the error message is:
foo.cxx:20:22: warning: multi-character character constant [-Wmultichar]
result += 'a[i]';
^~~~~~ |
So it's confused that you have mutiple characters to express a character constant.
You also need to add:
#include <string>
near the top to get the definition of the string class.
Finally, since string, cin and cout are all defined within a "namespace", you should add
using namespace std;
after your last #include line. Note that for complex programs, it's frowned upon to use the entire namespace, but for a beginner program I think it's okay.
When I make these changes to your code, it works.