gpt4 book ai didi

if语句中的c++字符串比较

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:12 26 4
gpt4 key购买 nike

我有一个关于简单的 C++ 函数的问题。这是我的 cpp 文件:

#include <string>
#include <iostream>

#include <stdio.h>
#include <ros/ros.h>
#include <json_prolog/prolog.h>

using namespace std;
using namespace json_prolog;

int main(int argc, char *argv[])
{
ros::init(argc, argv, "Knives");

Prolog pl;
char M[]="drawer";
cout<<M<<endl;
if(strcmp(M,"knife")==0)
{
string q= "rdfs_individual_of(M, 'http://knowrob.org/kb/knowrob.owl#TableKnife')";



PrologQueryProxy bdgs = pl.query(q);

cout<< endl;

for(PrologQueryProxy::iterator it=bdgs.begin(); it != bdgs.end(); it++)
{
PrologBindings bdg = *it;
cout << "Knives individuals= "<< bdg["M"] << endl;
}

cout<< endl<< endl;
}


if(strcmp(M,"drawer")==0)
{
string q= "rdfs_individual_of(M, 'http://knowrob.org/kb/knowrob.owl#Drawer')";

PrologQueryProxy bdgs = pl.query(q);

cout<< endl;

for(PrologQueryProxy::iterator it=bdgs.begin(); it != bdgs.end(); it++)
{
PrologBindings bdg = *it;
cout << "Drawer individuals= "<< bdg["M"] << endl;
}

cout<< endl<< endl;
}

return 0;
}

此代码与 xml 文件连接以对其进行解析。如果我编译它工作,我没有问题。现在我必须改变它,因为我不想定义变量 char M 但我想在输入中给出它。问题是我改变了:

char M[]=....

与:

char M;
cin>>M;

我对从 char 到 const char [-fpermissive] 的转换有疑问

我该如何解决?

最佳答案

试试这个:

std::string M;
cin >> M;

像这样替换行:

if(strcmp(M,"drawer")==0)

用这个:

if (M == "drawer" )

关于if语句中的c++字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com