繁体   English   中英

如何将字符与字符串进行比较?

[英]how do I compare a character with a string?

#include <iostream>
#include <string>//needed to make string array
#include <fstream>//Needed for redaing in from external file
#include <cstdlib>//needed for rand() function (for random word)
#include <ctime>//needed for time() funtion to seed rand()
using namespace std;

/* 
    April 25,2020
    This program will take read in a random word from an external file and use it for a game of hang man */

void greeting();
void wordPick();

int main()
{
    char playAgain;
    int tries;
    char playerGuess;
    string secretWord;

    greeting();
    ifstream inFile("randwords.txt");
    if (inFile.is_open())
    {
        string wordlist[10];
        for(int i = 0; i < 10; ++i)
        {
            inFile >> wordlist[i];
        }
        srand(time(0));

        string secretword = wordlist[rand() % 10];
        cout << secretword << endl;
        inFile.close();
    }

    //wordPick();

    //ask user to guess a letter
    cout << "Please guess a letter: " << endl;
    cin >> playerGuess;
    if (playerGuess == secretWord[0])
    {
        cout << "Thats correct!" << secretWord [0];
    }
    else if (playerGuess == secretWord[1])
    {
        cout << "thats correct!" << secretWord[1];
    }
    else if (playerGuess == secretWord[2])
    {
        cout << "Thats correct!" << secretWord[2];
    }

    cout << "Do you want to play again? Y or N: ";
    cin >> playAgain;
    while (playAgain != 'Y' && playAgain != 'y' && playAgain != 'N' && playAgain != 'n')
    {
        cout << "Invalid, please enter Y or N: ";
        cin >> playAgain;
    }

    return 0;
}

void wordPick()//reads in external file and puts it in an array for a library of words to randomly choose
{
    ifstream inFile("randwords.txt");
    if (inFile.is_open())
    {
        string wordlist[10];
        for(int i = 0; i < 10; ++i)
        {
            inFile >> wordlist[i];
        }
        srand(time(0));

        string secretword = wordlist[rand() % 10];
        // cout << secretword << endl;
        inFile.close();
    }
}

void greeting()//welcomes the player to the game and starts the game
{
    string name;

    cout << "Hello, Player welcome to Hangman! " << endl;
    cout << "What should I call you?" << endl;
    cin >> name;

    cout << "Hello " << name << ", in this game you will try to guess a random 3 letter word." << endl;
    cout << "You will have at least 6 ties to guess the word or else the drawing will complete. Good luck!" << endl;

我正在尝试做的是检查char playerGuess是否是string secretWord中的字符之一,但即使它是正确的,它也不会输出字母。 也许有一种更简单的方法来比较我只是想念的?

我想既然这仍然比较它应该可以工作,但它只是在猜到字母后结束程序。

您将随机选择的单词分配给secretwordw小),但判断基于secretWordW大),没有分配任何单词。

那个部分

        string secretword = wordlist[rand() % 10];
        cout << secretword << endl;

应该

        secretWord = wordlist[rand() % 10];
        cout << secretWord << endl;

另请注意,您不能在if块中声明secretWord ,否则法官部分将看不到该分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM