Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error: Invalid conversion from 'char' to 'const char*'

Tags:

c++

I'm completely new to C++ and I created this function:

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

But it returns to following error: invalid conversion from 'char' to 'const char*' [-fpermissive]. Can anyone help me understand what this means?

like image 355
LazySloth13 Avatar asked Apr 12 '13 10:04

LazySloth13


3 Answers

string compLetter = compWord[x];

compWord[x] gets char and you are trying to assign it to string, that's wrong. However, your code should be something like

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;
}
like image 123
ForEveR Avatar answered Sep 29 '22 08:09

ForEveR


string compLetter = compWord[x];

should be

char compLetter = compWord[x];

like image 21
Manoj Awasthi Avatar answered Sep 29 '22 08:09

Manoj Awasthi


On this line

string compLetter = compWord[x];

You're assigning a char to a string. Changing it to

char compLetter = compWord[x];

Should do the trick.

like image 45
SlxS Avatar answered Sep 29 '22 07:09

SlxS