fwackk
fwackk

Reputation: 97

C++ '==': no conversion from 'int' to 'std::string'

I am working on an assignment, so far I am asking the user whether they want to go first or not. I am wanting to check their input to make sure it matches the two values that I want.

I am getting errors about conversion while using Visual Studio such as:

'==': no conversion from 'int' to 'std::string'

no operator "==" matches these operands

Any support is greatly appreciated.

Upvotes: 2

Views: 407

Answers (3)

Bathsheba
Bathsheba

Reputation: 234635

The remedy is simple: you need to change 'yes' and 'no' to "yes" and "no", as the latter two are string literals with an implicit nul-terminator.

But let's consider why the compiler issues the error that it does.

The single quotations are used to denote a character literal. Surprisingly perhaps, an implementation is allowed to permit character literals containing more than one char:

C11++ standard, §2.14.3/1 - Character literals

An ordinary character literal that contains more than one c-char is a multicharacter literal . A multicharacter literal has type int and implementation-defined value.

So 'yes' and 'no' have type int. Since the std::string class does not allow assignment to an int type it issues the error that you see.

Upvotes: 2

Tyler Lewis
Tyler Lewis

Reputation: 881

Your problem is in the line:

if (turnChoice == 'yes' || turnChoice == 'no')

C++ and C denote single characters with the single quotation marks, not strings of characters. The compiler is thus attempting to convert your single quotation marks into an integer value. You must use double quotations for string literals. So change the above line to:

if (turnChoice == "yes" || turnChoice == "no")

Upvotes: 7

Mo Abdul-Hameed
Mo Abdul-Hameed

Reputation: 6110

The issue is here: if (turnChoice == 'yes' || turnChoice == 'no') You should use double quotes around strings, like this: if (turnChoice == "yes" || turnChoice == "no").

Upvotes: 2

Related Questions