可能重复:
ISO C++ forbids comparison between pointer and integer [-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;
}
试图制作一个超级简短的文字竞速游戏,但它不起作用! 我的错误是:
24 error:no matching function for call to 'std::basic_istream<char>::getline(std::string&)'
并且每个单行都有回复if
我得到的东西
C ++禁止在指针和整数
我不是试图以任何形式或形式o_o这样做
没有什么能帮助解决它。人们似乎让它工作,因为他们使用""
而不是''
,他们将其更改为''
,并且工作正常。但我总是使用''
!
答案 0 :(得分: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
}
}
答案 1 :(得分:2)
只需使用strcmp,阅读有关数组和解除引用的内容,并知道'A'表示ASCI大写字母A,其中“A”表示字符串,即['A','null']。