簡體   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