2014-09-06 39 views
-1

所以,即時嘗試編寫一個文本冒險的事情,每當我嘗試比較一個字符串與消息,我總是會得到相同的輸出,無論我寫什麼.. 。我已經搜索和搜索,並且找不到答案...我有使用Java,LUA和一些HTML編程的經驗......也許我嘗試做的事與Java的方式太類似了?那麼,這裏有一些可能最終有助於幫助的代碼。C++ if語句字符串只給出一個答案

string sex; 

cout << "Would you like to be a boy or a girl?" << endl; 
cin >> sex; 
cout << sex; 
//statement to declare what you are... 
if (sex == "boy" || "Boy"){ 
    cout << "You have chosen to be a boy!" << endl; 
} 
else if (sex == "girl" || "Girl"){ 
    cout << "You have chosen to be a girl!" << endl; 
} 
else{ 
    create(); 
} 

任何形式的幫助將不勝感激。謝謝。

+0

c中的字符串是不同的。嘗試使用['strcasecmp'](http://linux.die.net/man/3/strcasecmp) – DOOM 2014-09-06 03:24:17

回答

1

變化

if (sex == "boy" || sex == "Boy"){ 
    cout << "You have chosen to be a boy!" << endl; 
} 
else if (sex == "girl" || sex == "Girl"){ 
    cout << "You have chosen to be a girl!" << endl; 
} 

if (sex == "boy" || sex == "Boy"){ 
    cout << "You have chosen to be a boy!" << endl; 
} 
else if (sex == "girl" || sex =="Girl"){ 
    cout << "You have chosen to be a girl!" << endl; 
} 

原因是if (sex == "boy" || "Boy")聲明,第二部分將始終評估爲真。