AGasior AGasior -4 years ago 204
C++ Question

Concatenate string with ASCII

I want to achieve something like this:
(A) (B) (C) (D)... (I) (J)

for (int i = 1; i <= 10; i++){
battlefield[i][0] = "(" + (64 + i) + ")";

I'v up with an idea to just put ASCII value into String, but it causes error:

Expression must have integral or unscoped enum type

if I try to compile it I got

error C2110: '+' : cannot add two pointers

Basicly, the only way to make this compile is to add to_string before (64+i), but then I got something like:
(65) (66) (67), while I want letters which are under those ASCII values

Answer Source

You have to convert string literals to std::string before using operator+. A way of doing this

std::vector<std::string> battlefield(10);

for (char i = 0; i < 10; i++)
    battlefield[i]  = "(";
    battlefield[i] += 'A' + i;
    battlefield[i] += ")";


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download