Hello,

I'm fairly new to C++, and I am currently doing a CS assignment, which is to create a hangman game. The only problem i'm having so far is that i'm getting an error when i try to pass a char array to a function (invalid conversion from 'char*' to 'char'). i want to pass the char array into a function to compare with a letter. I believe the way i'm doing it right now is trying to pass the pointer..

here is my code...

//written by iluwinter

#include<iostream>
#include<cstring>

using namespace std;

void readFunction(char,int&);
void guessLetter(char&, int&);
void checkLetter(char, char, char&, int&);
int  rightORwrong(char&, char&, char&, int&);
void printSolution(char, int&);

int main()
{
    char word[100], solution[100], letter;
    int length=0, maxtries=0;
    int i, guess;

    readFunction(word,maxtries);

    length = strlen(word);

    for(i=0;i<length;i++)
    {
        solution[i]="*";
    }

    do
    {
    guessLetter(letter,maxtries);
    
    guess = rightORwrong(word, solution, letter, length);
    
    checkLetter(word, solution, letter, length);

        if(guess==2)
        {
            cout<<"Right! Word so far: ";
            printSolution(solution, length);
        }
        else
            if(guess==4)
            {
                cout<<"Letter already guessed"<<endl;
            }
            else
            {
                cout<<"Wrong! Try again. Word so far: ";
                printSolution(solution, length);
            }

    }while(!strcmp(word,solution) && maxtries != 0);

    if(!strcmp(word,solution) && maxtries==0)
    {
        cout<<"You lose!"<<endl;
    }
    else
    {
        cout<<"You win!"<<endl;
    }

    return 0;
}

void readFunction(char word[],int& maxtries)
{
    cout<<"Enter the word to be guessed: ";
    cin.getline(word, 100,'\n');
    cout<<endl<<"Enter the max number of tries: ";
    cin>>maxtries;
    cout<<endl;
}    

void guessLetter(char& guess,int& max)
{
    cout<<"Guess a letter (you have "<<max<<" tries left): ";
    cin>>guess;
    cout<<endl;
    max=(max-1);
}

void checkLetter(char word[],char solution[], char& letter, int& length)
{
    int j;

    for(j=0;j<length;j++)
    {
        if(word[j]=letter)
        {
            solution[j]=letter;
            
        }
    }

}

int rightORwrong(char word[],char solution[], char& letter, int& length)
{
    int i;

    for(i=0;i<length;i++)
    {
        if(letter==word[i])
        {
            return 2;
        }
        else
            if(letter==solution[i])
            {
                return 4;
            }
            else
            {
                return 6;
            }
    }
}

void printSolution(char solution[],int& length)
{
    int i;

    for(i=0;i<length;i++)
    {
        cout<<solution[i];
    }
    cout<<endl;
}

any suggestions would be great. thanks so much!!

Edited 6 Years Ago by iluwinter: n/a

#include<iostream.h>
#include<cstring.h>
#include<conio.h>

//using namespace std;

void readFunction(char*,int&);
void guessLetter(char&, int&);
void checkLetter(char*, char*, char&, int&);
int  rightORwrong(char*, char*, char&, int&);
void printSolution(char*, int&);

int main()
{
    char word[100], solution[100], letter;
    int length=0, maxtries=0;
    int i, guess;

    readFunction(word,maxtries);

    length = strlen(word);

    for(i=0;i<length;i++)
    {
        solution[i]='*';
    }

    do
    {
    guessLetter(letter,maxtries);

    guess = rightORwrong(word, solution, letter, length);

    checkLetter(word, solution, letter, length);

        if(guess==2)
        {
            cout<<"Right! Word so far: ";
            printSolution(solution, length);
        }
        else
            if(guess==4)
            {
                cout<<"Letter already guessed"<<endl;
            }
            else
            {
                cout<<"Wrong! Try again. Word so far: ";
                printSolution(solution, length);
            }

    }while(!strcmp(word,solution) && maxtries != 0);

    if(!strcmp(word,solution) && maxtries==0)
    {
        cout<<"You lose!"<<endl;
    }
    else
    {
        cout<<"You win!"<<endl;
    }

   // return 0;
    getch();
}

void readFunction(char word[],int& maxtries)
{
    cout<<"Enter the word to be guessed: ";
    cin.getline(word, 100,'\n');
    cout<<endl<<"Enter the max number of tries: ";
    cin>>maxtries;
    cout<<endl;
}

void guessLetter(char& guess,int& max)
{
    cout<<"Guess a letter (you have "<<max<<" tries left): ";
    cin>>guess;
    cout<<endl;
    max=(max-1);
}

void checkLetter(char word[],char solution[], char& letter, int& length)
{
    int j;

    for(j=0;j<length;j++)
    {
        if(word[j]=letter)
        {
            solution[j]=letter;

        }
    }

}

int rightORwrong(char word[],char solution[], char& letter, int& length)
{
    int i;

    for(i=0;i<length;i++)
    {
        if(letter==word[i])
        {
            return 2;
        }
        else
            if(letter==solution[i])
            {
                return 4;
            }
            else
            {
                return 6;
            }
    }
}

void printSolution(char solution[],int& length)
{
    int i;

    for(i=0;i<length;i++)
    {
        cout<<solution[i];
    }
    cout<<endl;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The problem is function declaration. You have declared the functions to be accepting a char, instead of a char* though you have defined them properly. Correct the function declarations and it should work.

This article has been dead for over six months. Start a new discussion instead.