-1

可能重复:
ISO C++ 禁止指针和整数之间的比较 [-fpermissive]| [c++]

#include <iostream>

using namespace std;

int main()
{
char name[20];
char color[20];
char response[20];
int age;
    cout << "What is your name?\n";
    cin.getline(name, 20); cout << endl;
    cout << "What is your favorite color?\n";
    cin.getline(color, 20); cout << endl;
    cout << "How old are you?\n";
    cin >> age;
    cout << "Your name is " << name << ", your favorite color is " << color << " and you are " << age << " years old!\n";
    cin.get();
    cout << "You wake up from bed all you know is your name and age.\n";
    cout << "You are wearing a plain " << color << " t-shirt.\n";
    cout << "You see a gun on the table.\n";
    cout << "You see a door.\n";
    cout << "What do you do?\n";
    cin.getline(response, 20);
    if(response == 'Pick up the gun')
    {
        cout << "You pick up the gun.\n";
        cout << "Knock down the door with it? (Y/N)\n";
        cin.getline(response, 20);
        if(response == 'Y')
        {
            cout << "The door opens.\n";
            cout << "You see a zombie.\n";
            cout << "You see an open window.\n";
            cout << "What do you do?\n";
            cin.getline(response, 20);
            if(response == 'Shoot the zombie')
            {
                cout << "The zombie dies and it attracts other zombies.\n";
                cout << "GAME OVER!\n";
                cin.get();
                return 0;
            }
            else if(response == 'Jump out the window')
            {
                cout << "The zombie does not hear you and you safely make it out!\n";
                cout << "VICTORY!\n";
                cin.get();
                return 0;
            }
        }
        else if(response == 'N')
        {
        }
    }
    else if(response == 'Open the door')
    {
        cout << "It appears to be locked.\n";
    }
    return 0;
}

试图制作一个超级超短和简单的文字 rping 游戏,但它不起作用!我的错误是:

24 error:no matching function for call to 'std::basic_istream<char>::getline(std::string&)'

并且在每一行都有if我得到的响应内容 C++ 禁止指针和整数之间的比较

我没有试图以任何形式或形式 o_o 这样做似乎无助于解决它。人们似乎让它工作,因为他们使用""而不是,''他们改变它'',它工作正常。但我一直在使用''

4

2 回答 2

3

对于 if 语句,您应该使用:

if(strcmp(response,"Pick up the gun"))

但实际上,如果您使用std::string

#include <string>
using namespace std;
int main()
{
  string name;
  string response;
  getline(cin, response);
  if !(response.compare("Pick up the gun"))
  {
    //Do stuff here for picking up gun
  }
}
于 2012-11-18T19:23:02.987 回答
2

只需使用 strcmp,阅读有关数组和取消引用的内容,并知道“A”表示 ASCI 大写 A,其中“A”表示字符串,即 ['A','null']。

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

于 2012-11-18T19:35:43.770 回答