Multimap 不接受玩家输入的名字

问题描述 投票:0回答:1

我目前正在尝试使用多地图实现排行榜,让玩家输入他们的名字。

当我给它一个普通字符串但由于某种原因不接受玩家输入的字符串时它可以工作,我知道玩家输入的字符串是正确的,因为我在控制台中打印它并且它是正确的但 printScore 函数没有打印它

LeaderBoard.h

#pragma once
#include <iostream>
#include <map>
#include <string>

class LeaderBoard
{
private:

    std::multimap<std::string, int> scores; 

public:
    LeaderBoard() : scores()
    {
    }

    void newScore(std::string playerName, int playerScore);

    void printScores();
};

排行榜.cpp

#include "LeaderBoard.h"
#include <iostream>
void LeaderBoard::newScore(std::string playerName, int playerScore)
{
    std::cout << "score added" << std::endl;
    scores.insert(std::make_pair(playerName, playerScore));
}

void LeaderBoard::printScores()
{
    std::cout << "Player Scores:" << std::endl;
    for (auto it = scores.rbegin(); it != scores.rend(); ++it)
    {
        std::cout << it->first << ": " << it->second <<std::endl;
    }
}

主要.cpp

LeaderBoard leaderBoard;
bool newScoreAdded = false;
bool nameFinished = false;
std::string playerName;
std::string EnteredPlayerName;
std::string testname = "tester";

leaderBoard.newScore(testname, 10);

while (window.isOpen())
{
   while (window.pollEvent(event))
   {
       if (event.type == sf::Event::Closed) {
        window.close();
       }
       else if (event.type == sf::Event::TextEntered && player.playerDead == true) {
           if (event.text.unicode < 128) {
               if (event.text.unicode == '\b' && EnteredPlayerName.size() > 0) {
                EnteredPlayerName.pop_back();
               }
               else if (event.text.unicode != '\b' && event.text.unicode != '\n') {
                EnteredPlayerName += static_cast<char>(event.text.unicode);
            }
        }
    }
    
}

if (nameFinished == true && newScoreAdded == false)
{

    playerName = EnteredPlayerName;
    cout << "player name: " << playerName << endl;
    leaderBoard.newScore(playerName, EnemiesKilled);
    leaderBoard.printScores();
    newScoreAdded = true;
}

if (player.playerDead == true && nameFinished == false)
{
    window.draw(EnterNameHere);
    playerNameText.setString(EnteredPlayerName);
    playerNameText.setPosition(EnterNameHere.getPosition().x + 80, EnterNameHere.getPosition().y + 25);
    window.draw(playerNameText);

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
    {
        nameFinished = true;
    }

  }
}

将打印所有计数并打印测试者分数,但仅打印玩家输入的分数,而不是名称

c++ stl sfml
1个回答
0
投票

这个问题是我使用了回车键,按回车键会生成一个新行,因此它无法识别该字符串,而是选择制作一个用于提交玩家姓名的按钮

© www.soinside.com 2019 - 2024. All rights reserved.