我的Hangman代码有问题。
这是我得到的错误:
错误:无法将参数'1'的'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'转换为'char'到'int checkGuess(char,std :: __ cxx11 :: string, std :: __ cxx11 :: string&)'
从来没有真正看到过这样的错误。
这是我的代码:
#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;
}
答案 0 :(得分:0)
checkGuess
采用char
类型作为第一个参数,但是您提供的letter
实际上是std::string
类型。该代码将无法编译。
您可以改为阅读letter[0]
来获取字符串的第一个char
。首先确保letter.length()
大于0
。