我的If语句不接受'=='

时间:2014-03-06 22:06:26

标签: c++ if-statement

我做了一个小游戏,程序乱七八糟地要求玩家输入。但是,其中一个If语句给了我一个错误并阻止我编译程序。

string isready; cin>>的isReady;

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

上面我设置了一个名为isready的字符串,而不是要求用户输入。如上所示, 如果输入和接收y或y,我希望if语句激活。 但是,它只是给我错误:

二进制表达式的无效操作数('string'       (又名'basic_string,allocator>')和'int')

也许我错过了#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;
}

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

这里

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

您正试图在operator==std::string上使用char,因为'y'是char。除此之外,条件应在括号中,因为||的优先级低于==

正确的版本是:

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

答案 2 :(得分:0)

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

你应该单独检查每个角色。

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

答案 3 :(得分:0)

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

应该是

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

答案 4 :(得分:0)

更改此声明

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

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

考虑到有双引号。

问题是没有这样的operator ==可以将std :: string类型的对象与char类型的对象进行比较。类std :: string中没有这样的构造函数可以隐式地将char类型的对象转换为std :: string类型的对象。但是,类std :: string有一个构造函数,可以将字符串文字转换为std:string类型的对象。因此,“y”或“y”的右操作数被隐式转换为std :: string类型的临时对象。结果在上面的条件中比较了两个std :: string类型的对象。

即使你使用字符串文字而不是字符文字,你最初写的条件也是无效的。如果例如isready == "y"等于false,那么您将获得

false || "y"

在此表达式中,字符串文字“y”将转换为指向其第一个字符的指针。由于此指针不等于NULL,因此整个表达式将true与isready中的值无关