C ++字符串返回一个额外的字符

时间:2018-06-28 03:06:53

标签: c++ string

我已经测试了程序,可以肯定的是,返回的函数中的字符串等于“ card001”。但是返回的值等于“ card0011”。我不知道这怎么发生。在我迷失方向之前帮助我。 ;)

std::string function_cardTexture(int card) {

    //removes the last 1
    card = card - 10000;

    int ctr = 0;

    card = floor(card / 10);

    std::cout << card << std::endl;

    //turn int card into a string
    std::string a = static_cast<std::ostringstream*>(&(std::ostringstream() << card))->str();

    //combines card and string a into one string
    std::string nametext = "card00" + a;

    std::cout << nametext << std::endl;

    return (nametext);
}

void function_Battle(tempPlayer &Player, tempCard &card001) {

    if (Player.Start == true) {
        //Draw hand
        for (int i = 0; i < Player.numDrawn; i++) {

            int x = rand() % Player.deckSize + 0; ;

            Player.Hand[i] = Player.Deck[x];
            Player.Discarded[x] = 1;
        }

        Player.Start = false;
    }
    std::map<std::string, tempCard> Vars;
    //draw hand
    for (int i = 0; i < Player.handMax;i++) {
        if (Player.Hand[i] != 0) {
            sf::RectangleShape Card(sf::Vector2f(80.0f, 128.0f));

            std::string nametext = function_cardTexture(Player.Hand[i]);
            std::cout << nametext;
            sf::Texture texture = Vars[nametext].Art;

            Card.setTexture(&texture);
            window.draw(Card);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是,如何在function_Battle()函数中不使用换行符的情况下打印出内容,因此您很可能会将新值与旧值“粉碎”在一起。如果您仅用一个循环来替换主函数,就可以更清晰地打印值,那么您会发现您没有问题: http://coliru.stacked-crooked.com/a/8d1e4f51643b84b9

该链接将转到在线编译器,在该编译器中,我只是将调用函数替换为产生数字的循环。它甚至提供负数。