2014-03-06 62 views
-4

我做了一個小遊戲,程序對一個單詞進行拼寫並要求玩家輸入。然而,If語句之一給我一個錯誤,阻止我編譯程序。我的if語句不接受'=='

string isready; cin >> isready;

如果(的isReady == 'Y' || 'Y')

在上面,我成立了一個字符串被稱爲的isReady,不是要求用戶進行輸入。如上所見, 我希望if語句在y或大寫字母y被輸入和接收時激活。 然而,它只是給我的錯誤:

無效操作數爲二進制表達式(「串」 (又名「basic_string的,分配器>」)和「廉政」)

也許我錯過的#include文件?

#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <ctime> 
#include <unistd.h> 
using namespace std; 

int main() 

{ 

enum fields {WORD, HINT, NUM_FIELDS}; 
const int NUM_WORDS = 5; 
const string WORDS[NUM_WORDS][NUM_FIELDS] = //5x2 array 
{ 
    {"wall", "Do you feel you're banging your head against something?"}, 
    {"glasses", "These might help you see the answer."}, 
    {"labored", "Going slowly, is it"}, 
    {"persistent", "Keep at it."}, 
    {"jumble", "It's what the game is all about."} 
}; 

srand(static_cast<unsigned int>(time(0))); 
int choice = rand() % NUM_WORDS; 
//Choice value in array, than area in array where word and hint are 
string theWord = WORDS[choice][WORD]; //word to guess 
string theHint = WORDS[choice][HINT]; //hint for word 

string jumble = theWord; //jumbled version of word 
int length = jumble.size(); 
//Index1 and index2 are random locations in the string theWord 
//last two lines swaps areas, ending the for function with a different 
//jumble variable every time. 
for (int i = 0; i < length; ++i) 
{ 
    int index1 = rand() % length; 
    int index2 = rand() % length; 
    char temp = jumble[index1]; 
    jumble[index1] = jumble[index2]; 
    jumble[index2] = temp; 
} 

cout << "\t\tWelcome to Word Jumble!\n\n"; 
cout << "Unscramble the letters to make a word.\n"; 
cout << "\n\n\nReady? (y/n)"; 

//I'm asking here if the player is ready 
string isready; 
cin >> isready; 

if (isready == 'y' || 'Y') 
{ 
    cout << "Ok this is how the scoring works\n"; 
    cout << "The length of the word you will guess is times by 5000.\n"; 
    cout << "If you ask for a hint, your score will go down by half.\n"; 
    cout << "If you get the wrong answer, your score will go down by 1000."; 
    cout << "\nOk, lets start!\n\n\n"; 
    int counter = 3; 
    for(int i = 0; i < 3; ++i) 
    { 

     cout << counter << "..." << endl; 
     counter--; 
    } 

    sleep(1); 
} 
else 
{ 
cout << "check"; 
} 
cout << "Enter 'quit' to quit the game.\n"; 
cout << "Enter 'hint' for a hint.\n"; 
cout << "The jumble is: " << jumble; 

//Score system 
unsigned long int score; 
int amount_of_guesses, amount_of_wrong = 0; 




string guess; 
cout << "\n\nYour guess: "; 
cin >> guess; 

while ((guess != theWord) && (guess != "quit")) 
{ 
    if (guess == "hint") 
    { 
     cout << theHint; 
     amount_of_guesses++; 

    } 

    else 
    { 
     cout << "Sorry, that's not it."; 
     amount_of_wrong++; 
    } 

    cout << "\n\nYour guess: "; 
    cin >> guess; 
} 
score = theWord.length() * 1000 -(amount_of_wrong * 1000) 
/2 * amount_of_guesses; 
if (guess == theWord) 
{ 
    cout << "\nThat's it! You guessed it!\n"; 
} 

cout << "Your score is: " << score; 
cout << "\nThanks for playing.\n"; 
return 0; 
} 

回答

0
(isready == 'y' || 'Y') 

您應該檢查每個字符seperately。

((isready == "y" || (isready == "Y")) 
+0

這不會編譯,@Mahesh請你能downvote自己的答案 – 4pie0

+1

@lizusek啊..沒有注意到我沒有使用雙引號。謝謝。 – Mahesh

1

運營商||需要邏輯表達式兩側:

if (isready == "y" || isready == "Y") 

注意上面的雙引號,因爲isreadystd::string。您還可以將isready更改爲char,並使用字符常量(即單引號中的'y''Y')。

您當前的表達在語法上是有效的,但如下它將被評爲無條件true,因爲它被解釋:

if (isready == 'y' || 'Y' != 0) 
//     ^^^^^^^^ 
//     != 0 part is implicit; 
//    `Y` != 0 is always true, so the entire OR is also always true 
+0

這不會編譯 – 4pie0

+0

@lizusek請問你能更具體地指出什麼部分不能編譯? – dasblinkenlight

+0

比較std :: string到char – 4pie0

0
if (isready == 'y' || 'Y') 

應該

if (isready == "y" || isready == "Y") 
0

更改本聲明

if (isready == 'y' || 'Y') 

if (isready == "y" || isready == "Y") 

考慮到有雙引號。

問題是沒有這樣的運算符==可以比較std :: string類型的對象與char類型的對象。 std :: string類中沒有這樣的構造函數,它可以將char類型的對象隱式轉換爲std :: string類型的對象。然而class std :: string有一個構造函數,它可以將字符串文字轉換爲std:string類型的對象。因此,將「y」或「y」的右操作數隱式轉換爲std :: string類型的臨時對象。作爲上述條件的結果,比較了std :: string類型的兩個對象。

此外,即使您使用字符串文字而不是字符文字,最初編寫的條件也是無效的。例如,如果isready == "y"等於false那麼你會得到

false || "y" 

在這個表達式字符串常量「Y」被轉換成一個指向它的第一個字符。由於該指針不等於NULL,則整個表達式會在的isReady

+0

這修復了我的程序,謝謝。但是,爲什麼雙引號能夠解決這個問題,單字母字符不需要單引號? – user3361175

+0

@ user3361175在此表達式中,isready ==「y」編譯器搜索可應用的重疊運算符==。然而,沒有這樣的操作符將std :: string類型的對象與一個字符進行比較,因爲編譯器不能將一個char隱式轉換爲std :: string類型。然而,編譯器可以將字符串文字轉換爲例如「y」爲std :: string類型的對象,因爲std :: string中存在一個合適的構造函數,它將字符串文字作爲參數。所以實際上你比較了std :: string類型的兩個對象。 –

+0

@ user3361175另請參閱我更新的帖子。 –

1

這裏

(isready == 'y' || 'Y') 

你嘗試在std::stringchar使用operator==價值true independing,因爲「Y」是char 。除了這個條件應該是在括號中,因爲||具有優先級低於==

正確的版本是:

((isready == "y") || (isready == "Y")) // use bool operator== 
                 (const string& lhs, 
                  const string& rhs); 
+1

+1糾正我:) – Mahesh