fwackk fwackk - 27 days ago 6
C++ Question

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

here is my code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string turnChoice;
cout << "Would you like to go first?" << endl;
cout << "Enter 'yes' or 'no'." << endl;
cin >> turnChoice;

int i = 0;
while (i < 1)
{
if (turnChoice == 'yes' || turnChoice == 'no')
{
i = i + 1;
}
else
{
cout << "You must enter 'yes' or 'no'." << endl;
}
}
cout << "Thanks." << endl;
return 0;
}


Any support is greatly appreciated.

Answer

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")
Comments