c++ string comparison in if statement

Marcello Chiurazzi

i have a question about a simple c++ function. This is my cpp file:

#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;
}

this code is connect with an xml file to parse it. if i compile it works and i have no problems. Now i have to change it,because i don't want to define the variable char M but i want to give it in input. the problem is that i change :

char M[]=....

with:

char M;
cin>>M;

i have a problem about the conversion from char to const char [-fpermissive]

how i can solve it?

Richard Hodges

try this:

std::string M;
cin >> M;

replace lines like this:

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

with this:

if (M == "drawer" )

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Date comparison in case statement

분류에서Dev

Multiple String Comparison in Bash

분류에서Dev

exception in string comparison with empty string

분류에서Dev

Segfault on char comparison in C

분류에서Dev

Errors in binary comparison in C

분류에서Dev

String comparison issue in bash script

분류에서Dev

Improve Performance of VBA String Comparison

분류에서Dev

java String comparison interesting bug

분류에서Dev

String comparison using in doesn't work with unless

분류에서Dev

What's wrong with this string equality comparison? (Python)

분류에서Dev

How to do string comparison properly in shell script?

분류에서Dev

Switch statement is not accepting String variable

분류에서Dev

while with string and tow statement promblems

분류에서Dev

Initializing multiset with custom comparison function in C++

분류에서Dev

Best way for key comparison (C++ map)

분류에서Dev

PHPUnit "No Tests Executed" if I enable test string comparison

분류에서Dev

Word translator program based on string comparison - heap memory assertion fails

분류에서Dev

Switch statement error in C++

분류에서Dev

Understanding if statement syntax in c#

분류에서Dev

Unexpected T_STRING error in a select statement?

분류에서Dev

Confusing JavaScript statement about string Concatenation

분류에서Dev

My logic is failing me (C++, nslookup, char comparison)

분류에서Dev

C++ return statement strange behaviour?

분류에서Dev

c# If else statement with case insensative

분류에서Dev

How to get scanf to work in an if statement objective c

분류에서Dev

C# and LINQ - arbitrary statement instead of let

분류에서Dev

Log each statement of SqlDataAdapter C#

분류에서Dev

Switch Statement - Nested Functions - C++

분류에서Dev

Fix query to resolve to_char and or string comparison issue in scala databricks 2.4.3

Related 관련 기사

뜨겁다태그

보관