1

How do I insert an integer i into a string? I tried this, but it doesn't work:

for (int i = 0; i < maxFrameBoss1; i++) {
    boss1Image[i] = al_load_bitmap("boss1//frame_0" + i + "_delay-0.03s.png");
}
2
  • @François When I put it in the beginning of the line it says namespace "std" has no member "string" Commented Jan 25, 2018 at 18:46
  • 1
    You must have forgotten #include <string>. Commented Jan 25, 2018 at 18:46

4 Answers 4

5

You need to convert i to a string first. Then concatenate the strings. Finally (apparently) get a pointer to char for your al_load_bitmap:

for (int i = 0; i < maxFrameBoss1; i++) {
    std::string filename = "boss1//frame_0" + std::to_string(i) + "_delay-0.03s.png";
    boss1Image[i] = al_load_bitmap(filename.c_str());
}
Sign up to request clarification or add additional context in comments.

3 Comments

why not leave it implicit ? std::string filename = "boss1//frame_0" + i + "_delay-0.03s.png";
Ok. So it says: 'ALLEGRO_BITMAP *al_load_bitmap(const char *)': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'const char *'
@MohammadKanan: Because it won't work. You can add an integer to a pointer, so "boss1//frame_0 + i yields a result--but the result is that pointer advanced by i positions, so if (for example) i = 10, that much basically yields: "me_0". Trying to add that to another string literal won't work.
4

How do I insert an "i" variable into a string.

There is a standard library function to convert an int to a std::string. See http://en.cppreference.com/w/cpp/string/basic_string/to_string.

Use

std::string filename = std::string("boss1//frame_0") + std::to_string(i) + "_delay-0.03s.png";
boss1Image[i] = al_load_bitmap(filename);

If al_load_bitmap cannot use a std::string, use:

std::string filename = std::string("boss1//frame_0") + std::to_string(i) + "_delay-0.03s.png";
boss1Image[i] = al_load_bitmap(filename.c_str());

4 Comments

I'm a little curious; how would you feel about al_load_bitmap(("boss1//frame_0" + std::to_string(i) + "_delay-0.03s.png").c_str())?
@Justin, I prefer to use two lines. It's easier to understand and maintain.
Ok. Now it says: 'ALLEGRO_BITMAP *al_load_bitmap(const char *)': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'const char *'
@mmarcin8, looks like you need the second version of my suggested solution.
4

The easy way to construct strings from arbitrary input types is to use std::ostringstream like so:

for (int i = 0; i < maxFrameBoss1; i++) {
    std::ostringstream bitmap_filename_builder;
    bitmap_filename_builder << "boss1//frame_0" << i << "_delay-0.03s.png";
    boss1Image[i] = al_load_bitmap(bitmap_filename_builder.str().c_str());
}

When using this, take care about the lifetime of the temporarily created std::string variable returned by the std::ostrinsgstream::str() function. The result of the c_str() function may become a dangling pointer after execution of that statement. Be sure that the function you're calling takes a copy of that c-style string parameter, or just uses it in a strongly sequential manner and doesn't store that pointer as a state elsewhere.

Comments

1

The easiest solution is to just use std::to_string(i), but that could potentially lead to extra allocations, as std::string cannot know the final size in the intermediate + operations.

Instead, you may want to use absl::StrCat or some other variant:

for (int i = 0; i < maxFrameBoss1; i++) {
    std::string filename = absl::StrCat("boss1//frame_0", i, "_delay-0.03s.png");
    boss1Image[i] = al_load_bitmap(filename.c_str());
}

This is a relatively small performance point, but the solution is easy enough to be worth mentioning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.