我正在使用Go Fish作爲類項目。在這段代碼中,我試圖採取從球員卡的面值,並把它轉換成它的INT數值(2 - 10,J = 10,Q = 11 ...)將char *與字符串文字進行比較的問題
char valued[10];
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "What card would you like to ask " << _otherplayers[IndexOfPlayerChoice]->GetName() << " for? (2-10, J, Q, K, A) ";
cin >> valued;
int size = strlen(valued) +1;
char * temp = new char[size];
strcpy_s(temp, size, valued);
if (temp == "J" || temp == "j")
value = 11;
if (temp == "Q" || temp == "q")
value = 12;
if (temp == "K" || temp == "k")
value = 13;
if (temp == "A" || temp == "a")
value = 14;
if (temp == "2" || temp == "3" || temp == "4" || temp == "5" || temp == "6" || temp == "7" || temp == "8" || temp == "9" || temp == "10")
value = int(temp);
然而當我運行代碼時,它永遠不會落入任何if語句中。在調試器中它將顯示temp =「A」,但是當它到達if語句時,它將評估爲false。
任何想法爲什麼會發生這種情況?
編輯:嘗試使用STRCMP這樣
if (strcmp(temp, "J") == 0 || strcmp(temp, "j") == 0)
value = 11;
if (strcmp(temp, "Q") == 0 || strcmp(temp, "q") == 0)
value = 12;
if (strcmp(temp, "K") == 0 || strcmp(temp, "k") == 0)
value = 13;
if (strcmp(temp, "A") == 0 || strcmp(temp, "a") == 0)
value = 14;
else
value = int(*temp);
但STRCMP說,他有一個錯誤讀取字符串文字,我比較。
你在這裏沒有使用'std :: string' n你的代碼,只是'char *'?!?你還有什麼問題嗎? –
@πάνταῥεῖ他將char *與字符串文字 –
進行比較時遇到問題,您無法將字符串與此'== ==運算符 – mangusta