我正在使用 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/