Jongz Puangput
Jongz Puangput

Reputation: 5637

C++ invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

I wonder what happen here ?? Please advice

I'm not familiar with c++ can't I do like this ?

  char result = (msg[0] == 0) ? "OFF" : "ON";
  printf("A/C is " + result + "\n");  

Upvotes: 0

Views: 1279

Answers (4)

Some programmer dude
Some programmer dude

Reputation: 409432

First of all you can't simply use the addition operation to concatenate C-style strings, second of all you should be using std::cout for output:

std::cout << "A/C is " << result << '\n';

If you want to concatenate strings, at least one string have to be a std::string object.

Upvotes: 0

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33944

It looks like you should be using streams.

std::cout << "A/C is " << result << std::endl;

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 223153

You should use:

printf("A/C is %s\n", result);

Your result is actually declared with the wrong type; it should be char const *.

Even better (as mentioned in Ed's comment), you shouldn't be using printf for this. Do this instead:

std::cout << "A/C is " << result << "\n";

Upvotes: 3

David Grayson
David Grayson

Reputation: 87486

Apparently you cannot. Try this:

const char * result = (msg[0] == 0) ? "OFF" : "ON";
printf("A/C is %s\n", result);

Upvotes: 1

Related Questions