android - 在 Cocos2d-x 中比较字符串时出错

标签 android c++ string cocos2d-x strcmp

我正在使用 cocos2d-x 创建一个 Android 游戏。

我在另一个类中调用一个静态函数来检查给定的用户名和密码是否正确。该函数返回一个字符串,如果用户名和密码都正确则返回“yes”,否则返回“no”。

然后我比较字符串以决定下一步做什么。我遇到的问题是,在 CCLOG 中它打印响应为"is",但是 if 分支不执行,即 strcmp(result.c_str(), “yes”) 不返回 true 但 CCLOG 打印结果为"is" ".

这是我的代码:

// LogInPage
if (((CCMenuItemSprite*) pSender)->getTag() == 0) {
    string username = string(m_pEditUsername->getText());
    string password = string(m_pEditPassword->getText());

    string result = SQLDatabase::logIn(username, password);
    CCLOG("RESULT IS %s", result.c_str());

    if (strcmp(result.c_str(), "yes") == 0) {
        CCLOG("IN IF BRANCH");
        CCDirector::sharedDirector()->replaceScene(PlayMultiplayerPage::scene());
    }
}

//静态登录函数

string SQLDatabase::logIn(string username, string password) {
    stringstream strm;
    strm << "http://****.net/select_functions.php?
function=logIn&username="
        << username << "&password=" << password;

CURL* curl;
CURLcode res;
string result;
curl = curl_easy_init();
string URL = strm.str();

if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Write_To_String);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    CCLOG("curl return code is %i", (int) res);
    CCLOG("response is \n %s", result.c_str());
}

return result;
}

//WRITE_TO_STRING 函数

size_t SQLDatabase::Write_To_String(void *ptr, size_t size, size_t count,
        void *stream) {
    ((string*) stream)->append((char*) ptr, 0, size * count);
    return size * count;
}

最佳答案

您可以为此使用特定于 C++ 的函数。 http://www.cplusplus.com/reference/string/string/compare/

如果不行

 char * cstr = new char [str.length()+1];
 std::strcpy (cstr, str.c_str());

然后比较一下。

关于android - 在 Cocos2d-x 中比较字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17127597/

相关文章:

c++ - wxGenericDirCtrl 错误 "wxTheFileIconsTable was nullptr"

java - 如何添加java输入文本

android - 最新的 ADT 不允许输入宽度和高度值

android - 多 Intent 处理由于方向改变

android - ionic 错误 : In <declare-styleable> FontFamilyFont, 无法找到属性 android:fontVariationSettings

c++ - 管理多个 UDP 套接字的超时

android object animator transition 和 alpahn

c++ - 而在std::cout , what does it work for?之前使用左移运算符(<<)

string - Str 增加了额外的空间

c - 这个从输入中读取数字的函数如何工作?