-2

I need to create file path, which is string, based on few other variables.

Deck::Deck() {
    char ranks[13] = {'2', '3', '4', '5', '6', '7', '8', '9', '10',
                        'J', 'Q', 'K', 'A'};
    char suits[4] = { 'c', 'd', 'h', 's' };

        for (int rank_index = 0; rank_index < 13; rank_index++) {
            for (int suit_index = 0; suit_index < 4; suit_index++) {
                std::string l_file_name = std::to_string(ranks[rank_index]) + 
                                       std::to_string(suits[suit_index]) + ".png";
                int l_value = rank_index + 2;
                if (ranks[rank_index] == 'J') l_value = 2;
                if (ranks[rank_index] == 'Q') l_value = 3;
                if (ranks[rank_index] == 'K') l_value = 4;
                if (ranks[rank_index] == 'A') l_value = 11;
                m_deck.push_back(Card(
                     ranks[rank_index], suits[suit_index], l_value, l_file_name));
            }
        }   
}

Card constructor looks like this

Card::Card(char p_rank, char p_suit, int p_value, std::string p_texture_file_name)
    :m_rank(p_rank), m_suit(p_suit), m_value(p_value) {
    m_texture.loadFromFile(FILE_PATH + p_texture_file_name);
    m_shape.setSize((sf::Vector2f(70, 90)));
    m_shape.setTexture(&m_texture);
}

and m_deck is just

std::vector<Card> m_deck;

I want to achieve string like this for example Qc.png (queen of clubs), etc, although I got some numbers into my string

5099.png Failed to load image "res\cards\5099.png". Reason: Unable to open file

For example. It doesnt convert my chars into string but into some numbers.

JohnDoe
  • 179
  • 1
  • 4
  • 17
  • 1
    _"It doesnt convert my chars into string but into some numbers."_ Of course it does. Converting numbers to strings is what `std::to_string()` is for, so it treats the input `char` as an integer, not a character. Did you read its documentation at all? To append `chars`, first create a string and then use `+=`, `.push_back()`, `.append(N, c)`, `+`, or etc. Again, the documentation will show all the different possibilities. – underscore_d Dec 14 '17 at 12:28
  • Possible duplicate of [How to append a char to a std::string?](https://stackoverflow.com/questions/1472048/how-to-append-a-char-to-a-stdstring) – underscore_d Dec 14 '17 at 12:30

2 Answers2

3

Your problem is std::to_string(ranks[rank_index]). std::to_string converts a number into a string. A char can be thought of as a character, but it can also be thought of as a small number which can hold at least 0-127.

So, on a typical ASCII implementation, std::to_string('2') will return a string containing "50".

What you need, is to construct a string containing a simple character. You can do that with: std::string{1, ranks[rank_index]} (or you can use parens (()), but using curly braces makes it clearer you are initializing a temporary).

Incidentally, rather than push_back, I would use emplace_back:

                m_deck.emplace_back(
                       ranks[rank_index], suits[suit_index], l_value, l_file_name);

It saves an unnecessary copy.

0

The std::to_string method converts a numeric type to its corresponding string, so the numbers you getting are the ASCII code for the corresponding chars. A quite straight forward way to accomplish your intention is to use a stringstream to fill the string:

// String stream definitions
#include<sstream>

for (int rank_index = 0; rank_index < 13; rank_index++) {
        for (int suit_index = 0; suit_index < 4; suit_index++) {
            std::string l_file_name;
            std::stringstream s_l_file_name;
            s_l_file_name << ranks[rank_index] << suits[suit_index] << ".png";
            s_l_file_name >> l_file_name;                         
            // Remainder of the code
        }
    }

Also, '10' is a multi-character constant, it will trigger a warning and lead to unexpected behavior, refer to this answer about multi-char constansts. You'll have to choose a single char to represent that rank.

WDiniz
  • 115
  • 2
  • 7