-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 に答える