0
votes

I have a problem with my Hangman code.

Here's the error what i get:

error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'char' for argument '1' to 'int checkGuess(char, std::__cxx11::string, std::__cxx11::string&)'

Never really seen any errors like this.

Here's my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <string>
#include <vector>


using namespace std;

int NUM_TRY = 15;
int checkGuess(char, string, string&);
void main_menu();
string word;

void beolvas(vector <string> &words);
void kiir   (vector <string> words);
void betolt (vector <string> words);
string message = "Play!";

int main(int argc, char *argv[])
{

vector <string> words;
string name;
string letter;
beolvas(words);
kiir(words);
betolt(words);

string hide_m(word.length(), '-');    

while(NUM_TRY != 0){
main_menu();
cout<<"\n\n\t\t\t\t" <<hide_m;
cout<<"\n\n\t\t\t\tMondj egy betut:  ";
getline(cin, letter);
if (checkGuess(letter, word, hide_m)==0)
 {
 message = "Incorrect letter!";
 NUM_TRY = NUM_TRY - 1;
 }
 if (word == hide_m);
 {
  cout<<"Congratulations, you guessed the word."<<endl;
  cout<<"\t\t\tThe word was "<<word<<endl;
  break;
 }
 if (letter == hide_m)
 {
   cout<<"Good job. You guessed a letter."<<endl;
     }
   }
 }
 void beolvas(vector <string> &words)
 {
   string sor;
   ifstream fin("words.txt");
   while(!fin.eof()){
   getline(fin, sor);
   words.push_back(sor);
  }
  fin.close();
 }

  void kiir(vector <string> words)
 {
  ofstream fout("olvasd.txt");
  srand(time(NULL));
  int n =rand() % 236;
  fout<<words[n]<<endl;
 }

  void betolt(vector <string> words)
  {
    ifstream input("olvasd.txt");
    while(!input.eof()) {
    getline(cin, word);
   }
  input.close();
   }

   int checkGuess(char guess, string secretword, string &guessword)
   {
   int i;
   int matches = 0;
   int len = secretword.length();
   for(i = 0; i < len; i++)
   {

    if(guess == guessword[i])
   {
    return 0;

  }
  if(guess == guessword[i])
   {
    guessword[i] = guess;
    matches++;
     }
    }
   return matches;
   }

   void main_menu()
    {
   system("color B");    //Light Aqua 
   system("cls");
   cout<<"\t\t\t\t\t";
   cout<<"\n\t\t\tHangman";
   cout<< "\n\t\tYou have" <<NUM_TRY <<" tries, to guess the word. ";
    cout<<"\n\n\t\t\t\t"+message;
 } 
1
The error means your int checkGuess(char, string, string&) prototype claims the function will accept a char data as its first argument, but you're passing it a std::string, and there is no suitable conversion from the latter to the former. And it's exactly accurate to what you're doing with the statement if (checkGuess(letter, word, hide_m), where letter is a rather oddly named std::string. - WhozCraig

1 Answers

0
votes

checkGuess takes a char type as first parameter, but you are providing letter which is actually of type std::string. This code won't compile.

You can read letter[0] instead to get the first char of the string. Make sure letter.length() is bigger than 0 first.