1. ## Can't add string literals.

I posted this problem in another post but seeing how that was for a different problem and I'd already been given a solution I thought I'd make a new post.
For some reason, I cannot add string literals using the + operator. I've tried tons of different functions such as copy(), strcat(), etc. BUT every friggin time, it either gives me error C2110 or it compiles and gives me a unreadable figure. I've tried conversions, I've tried everything that I know including 3 different compilers (Borland, VC, GNU GCC).

Would someone please find my dunce mistake so I can get on with my coding self?
If you need the compilable code please ask.

Code:
```#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

char getGrid()
{
char grid[6][6];
for ( int i = 0; i < 6; i++)
{
for ( int j = 0; j < 6; j++)
{
char* szResult = new char[255];
memset(szResult, 0x00, 255);
char szKey[] = "grid[" + i + "][" + j + "]"; // WILL NOT DO WHAT I NEED.
//This isn't the only option I've tried.
//Probably something easy I'm missing...

cout << szKey << " " << i << " " << j << endl; //int i and int j have normal values.
GetPrivateProfileString("Grid", szKey, "", szResult, 255, ".\\default.ini");
grid[i][j] = (int)szResult;
}

}
return **grid;
}```

2. You can't do this in C or C++.
Code:
`char szKey[] = "grid[" + i + "][" + j + "]"; // WILL NOT DO WHAT I NEED.`
But of course, you've figured that out by now!!

Define szKey as a C++ String and then try the *concatenation operator* (aka, the plus sign).

3. However, you can't use + on integers with strings - only strings and char arrays.

http://www.cppreference.com/wiki/str...ring_operators

One possible solution is to use stringstream, which is the C++ corresponding to sprintf.

--
Mats

4. matsp, thanks. This seems to work very well.

Popular pages Recent additions