我是C++的新手,我创建了这个函数:
bool guessWord(string compWord)
{
cout << "Guess a letter: ";
string userLetter;
cin >> userLetter;
for (unsigned int x = 0; x < compWord.length(); x++)
{
string compLetter = compWord[x];
if (compLetter == userLetter)
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但它回归到以下error: invalid conversion from 'char' to 'const char*' [-fpermissive].任何人都可以帮我理解这意味着什么?
string compLetter = compWord[x];
Run Code Online (Sandbox Code Playgroud)
compWord[x]getschar并且您试图将其分配给string,这是错误的。但是,您的代码应该类似于
bool guessWord(string compWord)
{
cout << "Guess a letter: ";
char userLetter;
cin >> userLetter;
for (unsigned int x = 0; x < compWord.length(); x++)
{
char compLetter = compWord[x];
if (compLetter == userLetter)
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29748 次 |
| 最近记录: |