怎么输入这个语句?

来源:百度知道 编辑:UC知道 时间:2024/06/22 08:44:48
如果是图书那么输出1
char title;
if(title="图书")
cout<<"1"<<endl;
我想C++编写如果title是图书的话就输出1
不过我这样写的是错的,请帮忙改一下

#include <iostream>
#include <string>
using namespace std;

int main(){
char title[20];
cin>>title;
if(!(strcmp(title,"图书")))
cout<<1<<endl;
cout<<title;
system("pause");
return 0;
}
//实际上用下面的方法会更好
#include <iostream>
#include <string>
using namespace std;

int main(){
string title;
cin>>title;
if(title=="图书")
cout<<1<<endl;
cout<<title;
system("pause");
return 0;
}

char title; //类型是字符,首先得改成字符数组比如:title[20]
if(title="图书") //字符串比较要用strcmp(s1,s2)字符串比较函数,书上有写的,自己去看看
cout<<"1"<<endl;

楼主的程序很好理解呀.怎么会不好理解呢,下面给出代码实现

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="图书";
if(s=="图书