免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4228 | 回复: 3
打印 上一主题 下一主题

请教 string compare 问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-22 03:17 |只看该作者 |倒序浏览
请教:
我有一个string command, 要用他来识别一些自己写的命令,例如:

cin >;>; command;
while(1) {
if (command.compare("help" ==0) {
     cout << "help message" << endl;
     command="";
}
if(command.compare("quit" ==0) {
     exit(0);
}
//这里有一个问题,要是我要比较的string是两个单词怎么办呢?
if(command.compare("print state" == 0) {
    printState();
    command = "";
}
}//end while

谢谢个位先

论坛徽章:
0
2 [报告]
发表于 2005-09-22 03:47 |只看该作者

请教 string compare 问题

几个函数原型如下:

  1. istream& getline( char* buffer, streamsize num );
  2. istream& getline( char* buffer, streamsize num, char delim );

  3. bool operator==(const string& c1, const string& c2);

  4. char buffer[256];
  5. cin.getline(buffer,256);//读入一行文字到缓冲区
  6. string command(buffer); //创建一个string对象
  7. if (command == "print state" ) //调用string的operator==重载友元函数
  8. {
  9.     cout << "command is print state" << endl;
  10. }
  11. else
  12. {
  13.     cout << "command is not print state" << endl;
  14. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2005-09-22 04:12 |只看该作者

请教 string compare 问题

不好意思,偶是新人一个. 还有一个问题,是跟上面问题一起的:如果说我的命令只有两个单词,之后还有一个 值 要pass进命令里面,例如:

set gear [gear]

命令是set gear,[gear]是一个int 值,用来pass into funtion.

刚才说的cin.getline(buffer,256) 是把整行读如了command,要是着种情况应该怎么半呢?

论坛徽章:
0
4 [报告]
发表于 2005-09-22 08:33 |只看该作者

请教 string compare 问题

顶一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP