Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
[Error] invalid conversion from 'char' to 'char*' [-fpermissive]

What I have tried:

C++
void Print_Hangman(void);
void askingName_and_rules(std::string name);
void printingWords_to_guess(char ** words, int size );
int  getNumOfLines(const char * fileName );
void fillWords(char ** words, const char * fileName );
void guess_it(char guess);
bool compairing_words(char * guess , char word);

//int tries()

int main(int argc, char** argv) 
{  
    std::string name;
    char guess;
    Print_Hangman(); 
    askingName_and_rules(name);
    const char fileName [] = "hangman.txt";
    const int numOfWords = getNumOfLines(fileName);
    if(numOfWords <= 0)
    {
        std::cout << "word file is missing in the game directory.\nExiting Game...\n";
        return 0;
    }

//  std::cout << numofwords << '\n';

    char ** words = new char*[numofwords];

    fillwords(words, filename);
    std::cout << "file has " numofwords;
    printingWords_to_guess(words,numofwords);
    guess_it(guess);
    if( compairing_words( guess, words ) )
    {
       cout << "hey\n";   // ??????
    }
    else
    {
       cout << "bye\n";   // ??????
    }
    return 0;
}

bool compairing_words(char * guess, word )
{
   for(int i=0; i < word->length(); i++ )
   {
      if(word[i]==guess)
      {
         return true;
      }
      return false;
   }
}

void guess_it(char guess)
{
    std::cout << " Guess a letter \n";
    std::cin >> guess;
}

/*void tries(int tries)
{
    tries=5;
    while(tries<=0)
    {
        guess_it(); 
    }
}*/


void Print_Hangman(void)
{
    std::cout << "\n";

    std::cout << "\t\t    H        H      A       N      N     G G G       " << "\n";
    std::cout << "\t\t    H        H     A  A     N N    N    G            " << "\n" ;
    std::cout << "\t\t    H HHHHHH H    A AA A    N  N   N   G     G G G   " << "\n" ;
    std::cout << "\t\t    H        H   A      A   N   N  N    G    G   G   " << "\n" ;
    std::cout << "\t\t    H        H  A        A  N    N N     G G G   G   " << "\n" ;

    std::cout << "\n";

    std::cout << "  \t\t\t    M       M        A       N      N    " << "\n";
    std::cout << "  \t\t\t    M M   M M       A  A     N N    N    " << "\n" ;
    std::cout << "  \t\t\t    M  M M  M      A AA A    N  N   N    " << "\n" ;
    std::cout << "  \t\t\t    M   M   M     A      A   N   N  N    " << "\n" ;
    std::cout << "  \t\t\t    M       M    A        A  N    N N    " << "\n" ;
}

void askingName_and_rules(std::string name)
{
    std::cout << " Enter your Name \n";
    std::cin >> name;
	
    std::cout << "\n\n So " << name << " before starting the game you have to follow some rules : ";
    std::cout << "\n\n 1: All letters should be small \n 2: you have 5 tries to guess word ";
    std::cout << "\n The word you have to guess is : \n";
}


void printingWords_to_guess(char ** words, int size )
{
    srand(time(NULL));
    int random_number = rand()%size;
    std::string word   = words[random_number];
    std::cout << word.replace(1,3,"_ _ _") << '\n';
    return;
}

int getNumOfLines(const char * fileName )
{
    int count =0 ;
    std::string line;
    std::ifstream file(fileName);
    while(std::getline(file,line)){
        count++;
    }
    file.close();
    return count;
}

void fillWords(char ** words, const char * fileName )
{
    std::ifstream file(fileName);
    std::string line;
    int i =0;
    while(std::getline(file, line))
    {
        words[i] = new char[line.length()];
        for(int j = 0; j < line.length(); ++j)
        {
            words[i][j] = line[j];
        }
        words[i][line.length()] = '\0';
        ++i;
    }
    file.close();
}
Posted
Updated 30-Jul-20 7:10am
v4
Comments
Richard MacCutchan 30-Jul-20 10:49am    
Please format your code properly, remove the duplicate, and explain exactly where the error occurs.
Patrice T 30-Jul-20 13:11pm    
And the position of error is ...
Information is in full error message.
Joe Woodbury 30-Jul-20 15:50pm    
I'm puzzled why you keep using 'new' instead of creating vectors of std::string.

There are other issues:

    
    while(std::getline(file, line))
    {
        words[i] = new char[line.length()];
        for(int j = 0; j < line.length(); ++j)
        {
            words[i][j] = line[j];
        }
        words[i][line.length()] = '\0';
        ++i;
    }

You are allocating line.length() and then assigning a terminating zero past the end of the allocation.

bool compairing_words(char * guess, word )
{
   for(int i=0; i < word->length(); i++ )
   {
      if(word[i]==guess)
      {
         return true;
      }
      return false;
   }
}


You return false after first check.

1 solution

One problem appears to be the guess function. It should return a character, not accept one as an argument. It should look something like this :
C++
char guess_it( void )
{
    char guess;
    std::cout << " Guess a letter \n";
    std::cin >> guess;
    return guess;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900