2

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

Weeedooo
  • 501
  • 8
  • 25

5 Answers5

1

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] += ")";
}

demo

Garf365
  • 3,619
  • 5
  • 29
  • 41
1

Your concatenation expression is incorrect, because it mixes types:

"(" + (64 + i) + ")"

You can fix it by making the middle part (64 + i) a single-character string instead of an int:

battlefield[i][0] = "(" + string(1, 64+i) + ")";

Constructor string(1, 64+i) makes a string from one repetition (first parameter) of character 64+i (second parameter). With this expression C++ has enough information to construct string objects from "(" and ")".

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

I think I'd be inclined to:

  1. Use zero-based indexing

  2. Use the character literal 'A' as a base in order to make the conversion explicit.

  3. hoist the generation of the string into a small function

Something like this:

#include <string>

auto& make_entry(std::string& cell, int index)
{
  cell += '(';
  cell += char('A' + index);
  cell += ')';
  return cell;
}

int main()
{
  std::string battlefield[10][1];
  for (int i = 0; i < 10; i++){
    make_entry(battlefield[i][0], i);
  }
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

You can try this :

battlefield[i][0] = std::string("(") + (char) ('A'+i) + std::string(")");

The reason of the error message is that, when you write "(", you are using a raw, C-style char*, and you cannot add two pointers (what your error message says). By using the std::string constructor, you are forcing values to be treated as strings, and then the + operator works for concatenation.

Fabien
  • 12,486
  • 9
  • 44
  • 62
0

You can just use the ASCII character contiguous layout for alphabetical characters, writing a for loop with a char index going from 'A' to 'J', and concatenating the current character to the result string.
During the concatenation, you can add the parentheses ( and ) as well.

E.g.:

#include <iostream>
#include <string>

int main() {
    std::string result;

    for (char ch = 'A'; ch <= 'J'; ++ch) {
        result += '(';
        result += ch;
        result += ')';
    }

    std::cout << result << '\n';
}

EDIT If you want a vector<string>, you can modify the aforementioned code, using something like this:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    vector<string> v;

    for (char ch = 'A'; ch <= 'J'; ++ch) {
        string s;
        s += '(';
        s += ch;
        s += ')';
        v.push_back(s);
    }

    for (auto const& s : v) {
        cout << s << ' ';
    }
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162