C++ 基于函数的字母猜测游戏

C++ 基于函数的字母猜测游戏,c++,function,C++,Function,我正在尝试为一个类项目做一个基于函数的字母猜测游戏。我以前完成了一个程序,将产生一个随机字母,现在我需要使其功能的基础上,但它不工作。任何帮助都将不胜感激 #include<iostream> #include<cstdlib> #include<ctime> using namespace std; void introduction(); //Display the introduction int getNumberOfGames(int&

我正在尝试为一个类项目做一个基于函数的字母猜测游戏。我以前完成了一个程序,将产生一个随机字母,现在我需要使其功能的基础上,但它不工作。任何帮助都将不胜感激

#include<iostream> 
#include<cstdlib>
#include<ctime> 
using namespace std;

void introduction();
//Display the introduction

int getNumberOfGames(int& number_of_games);
//Ask individual how many games they want to play
//

char compareTwoCharacters(char answer, char& guess);
//

char playOneGame(char answer);
//function returns true if user won;otherwise will return false. going to call the 3rd function to decide if correct or not



int main()
{
char alphabet [27];
int number_of_games;
char guess;
int games = 1;
char n = (char)(rand() % 26 + 1);
srand(time(0));


void introduction();
int getNumberOfGames(int);
char compareTwoCharacters(char,char);
char playOneGame(char);

    system("pause");

}

void introduction()
{
    cout<<"Weclome to the Letter Guessing game!\n";
    cout<<"You have 5 chances to guess each letter.\n \n";
    cout<<"Let's begin!\n";
    cout<<"**************************************************\n\n";

}

int getNumberOfGames(int& number_of_games)
{
    cout<<"How many games do you want to play? \n";
    cin >> number_of_games;
    return 0;

}

char compareTwoCharacters(char answer, char guess)
{
    cout << "Enter your guess: ";
    cin >> guess;
    if (guess == answer)
        return 0;
    else if (guess < answer)
        return 2;
    else
        return -2;
}

char playOneGame(char answer)
{
    //return true is guess = answer otherwise return false
    return true;

    while (games <= number_of_games) 
    char answer = 97 + rand() % 27;
    cout << "Lets play game " << games++ << '\n';
    for (int number_of_guesses = 0; number_of_guesses < 5; number_of_guesses++)
    {

        cout << "Enter your guess: ";
        cin >> guess;
        if (guess > answer)
        {
            cout << "The letter you are trying to guess is before " << guess << "\n";

        }
        else if (guess < answer)
        {
            cout << "The letter you are trying to guess is after " << guess << "\n";

        }
        else
        {
            cout << "Your guess is correct! \n";

            break;
        }

}
    cout << "The answer you were looking for was " << answer << '\n';
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效介绍();
//展示介绍
int getNumberOfGames(int和游戏的数量);
//询问个人想要玩多少游戏
//
字符比较字符(字符回答、字符和猜测);
//
char playOneGame(char回答);
//如果用户赢了,函数返回true;否则将返回false。将调用第三个函数来决定是否正确
int main()
{
字符字母表[27];
游戏的整数;
猜字符;
整数游戏=1;
字符n=(字符)(rand()%26+1);
srand(时间(0));
无效介绍();
int getNumberOfGames(int);
字符比较字符(char,char);
char游戏机(char);
系统(“暂停”);
}
无效导言()
{

cout代码中存在大量语法错误:

首先
srand(time(NULL))
函数应该出现在生成随机数之前,而不是之后。因此,您应该移动它,使代码看起来像:

srand(time(NULL));
// Add the rest of your code here
char n = (char)(rand() % 26 + 1);
其次,函数
playOneGame()中的while循环


while(games正如许多人已经说过的,您的代码中有很多语法错误

首先,在使用
rand()
函数之前,应该为随机值生成器设置种子(使用
srand()
)。因此,
srand(时间(0))
应该在
charn=(char)(rand()%26+1);
之前,而不是之后

Second调用函数时,不需要添加返回类型。例如,在
main()
函数中,函数
void introduction();
应更改为
introduction();
,与接下来的3个函数调用相同(请参阅:)

Third调用函数时,您需要一个参数,而不是类型。例如,您的函数调用
compareTwoCharacters(char,char);
不应将
char
作为其参数,而应具有如下变量或文字:
compareTwoCharacters(n,'a'));
。在编写的3个函数调用中进行这些更改

Forth,在
字符比较字符(字符回答,字符猜测)
(第57行)函数,您应该返回一个
char
,但在代码中,您返回了一个整数:
return 0;
return 2;
return-2;
。确保返回语句和返回类型匹配。这与
char playOneGame(char-answer)中的问题相同
函数;返回的是
true
,但返回类型是
char

Fifth,在您的
char playOneGame(char-answer)
函数中,您缺少while循环的括号。如果没有括号,它将只循环下一个命令,即
char-answer=97+rand()%27;
和永不结束(无限循环),因为从未达到退出条件

Sixth,在同一函数中,变量
game
从未声明。它是在不同的函数中声明的(
int games=1;
,在第26行),但一旦退出该函数,
game
将停止退出(有关详细信息:)

我修复了一些错误,试图保持代码的一般结构不变,并用
//CHANGED
标记了更改。但是,这可能不是您希望程序运行的方式:

#include<iostream> 
#include<cstdlib>
#include<ctime> 
using namespace std;

void introduction();
//Display the introduction

int getNumberOfGames(int& number_of_games);
//Ask individual how many games they want to play
//

int compareTwoCharacters(char answer, char guess); // CHANGED
//

int playOneGame(int games, int number_of_games, char guess); // CHANGED
//function returns true if user won;otherwise will return false. going to call the 3rd function to decide if correct or not



int main()
{
    char alphabet [27];
    int number_of_games;
    char guess;
    int games = 1;
    srand(time(0)); // CHANGED
    char n = (char)(rand() % 26 + 1); // CHANGED

    introduction(); // CHANGED
    getNumberOfGames(number_of_games); // CHANGED
    compareTwoCharacters(n,'A'); // CHANGED
    playOneGame(games, number_of_games, guess); // CHANGED

    system("pause");

}

void introduction()
{
    cout<<"Weclome to the Letter Guessing game!\n";
    cout<<"You have 5 chances to guess each letter.\n \n";
    cout<<"Let's begin!\n";
    cout<<"**************************************************\n\n";

}

int getNumberOfGames(int& number_of_games)
{
    cout<<"How many games do you want to play? \n";
    cin >> number_of_games;
    return 0;

}

int compareTwoCharacters(char answer, char guess) // CHANGED
{
    cout << "Enter your guess: ";
    cin >> guess;
    if (guess == answer)
        return 0;
    else if (guess < answer)
        return 2;
    else
        return -2;
}

int playOneGame(int games, int number_of_games, char guess) // CHANGED
{
    //return true is guess = answer otherwise return false
    // return true; // CHANGED
    char answer = 'a';  // CHANGED
    while (games <= number_of_games)
    { // CHANGED
        answer = 97 + rand() % 27;  // CHANGED
        cout << "Lets play game " << games++ << '\n';
        for (int number_of_guesses = 0; number_of_guesses < 5; number_of_guesses++)
        {

            cout << "Enter your guess: ";
            cin >> guess;
            if (guess > answer)
            {
                cout << "The letter you are trying to guess is before " << guess << "\n";

            }
            else if (guess < answer)
            {
                cout << "The letter you are trying to guess is after " << guess << "\n";

            }
            else
            {
                cout << "Your guess is correct! \n";

                break;
            }

        }
    } // CHANGED
    cout << "The answer you were looking for was " << answer << '\n';
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效介绍();
//展示介绍
int getNumberOfGames(int和游戏的数量);
//询问个人想要玩多少游戏
//
int compareTwoCharacters(字符回答,字符猜测);//已更改
//
int playOneGame(int games,int number of_games,char guess);//更改
//如果用户赢了,函数返回true;否则将返回false。将调用第三个函数以确定是否正确
int main()
{
字符字母表[27];
游戏的整数;
猜字符;
整数游戏=1;
srand(时间(0));//已更改
char n=(char)(rand()%26+1);//已更改
简介();//已更改
getNumberOfGames(游戏数);//已更改
CompareTocharacters(n,'A');//已更改
playOneGame(游戏,游戏数量,猜测);//更改
系统(“暂停”);
}
无效导言()
{

我想在你的代码中,你希望在
while(游戏)之后有一段代码。这段代码有重复的函数声明、无效的函数调用和语法错误。请阅读并修改。
while (games <= number_of_games)
{
    // Add necessary code
}
#include<iostream> 
#include<cstdlib>
#include<ctime> 
using namespace std;

void introduction();
//Display the introduction

int getNumberOfGames(int& number_of_games);
//Ask individual how many games they want to play
//

int compareTwoCharacters(char answer, char guess); // CHANGED
//

int playOneGame(int games, int number_of_games, char guess); // CHANGED
//function returns true if user won;otherwise will return false. going to call the 3rd function to decide if correct or not



int main()
{
    char alphabet [27];
    int number_of_games;
    char guess;
    int games = 1;
    srand(time(0)); // CHANGED
    char n = (char)(rand() % 26 + 1); // CHANGED

    introduction(); // CHANGED
    getNumberOfGames(number_of_games); // CHANGED
    compareTwoCharacters(n,'A'); // CHANGED
    playOneGame(games, number_of_games, guess); // CHANGED

    system("pause");

}

void introduction()
{
    cout<<"Weclome to the Letter Guessing game!\n";
    cout<<"You have 5 chances to guess each letter.\n \n";
    cout<<"Let's begin!\n";
    cout<<"**************************************************\n\n";

}

int getNumberOfGames(int& number_of_games)
{
    cout<<"How many games do you want to play? \n";
    cin >> number_of_games;
    return 0;

}

int compareTwoCharacters(char answer, char guess) // CHANGED
{
    cout << "Enter your guess: ";
    cin >> guess;
    if (guess == answer)
        return 0;
    else if (guess < answer)
        return 2;
    else
        return -2;
}

int playOneGame(int games, int number_of_games, char guess) // CHANGED
{
    //return true is guess = answer otherwise return false
    // return true; // CHANGED
    char answer = 'a';  // CHANGED
    while (games <= number_of_games)
    { // CHANGED
        answer = 97 + rand() % 27;  // CHANGED
        cout << "Lets play game " << games++ << '\n';
        for (int number_of_guesses = 0; number_of_guesses < 5; number_of_guesses++)
        {

            cout << "Enter your guess: ";
            cin >> guess;
            if (guess > answer)
            {
                cout << "The letter you are trying to guess is before " << guess << "\n";

            }
            else if (guess < answer)
            {
                cout << "The letter you are trying to guess is after " << guess << "\n";

            }
            else
            {
                cout << "Your guess is correct! \n";

                break;
            }

        }
    } // CHANGED
    cout << "The answer you were looking for was " << answer << '\n';
    return 0;
}