为什么我的布尔值一直显示为no而不是yes或no?

时间:2017-06-24 12:41:08

标签: c++ boolean

在这里学习C ++。如果你执行我的代码,它应该问你五个猜测,然后通过选择y为yes或n为no来要求你再次播放。但是,我很难选择两个选项出现“否”的地方,而且我不知道我的程序有什么问题。

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
bool AskToPlayAgain();
string GetGuess();


int main() 
{
// the entry point of our program
// calling a function declaration

PrintIntro();
PlayGame();
AskToPlayAgain();
return 0; // exit the application
}


string GetGuess() 
{
// get a guess from the player
cout << "Enter your guess: ";
string Guess = "";
getline(cin, Guess);

// print the guess back to them
cout << "Your guess was: ";
cout << Guess;
cout << endl;
return Guess;
}


void PrintIntro() 
{
// introduce a game
constexpr int WORD_LENGTH = 5;
cout << "Welcome to Bulls and Cow, a fun word game.\n";
cout << "Can you guess the " << WORD_LENGTH;
cout << " letter isogram I'm thinking of?\n";
cout << endl;
}

void PlayGame()
{
// loop five guesses of turn
constexpr int Number_Five = 5;
for (int x = Number_Five; x > 0; x--)
{
    string guess = GetGuess();
    cout << endl;
}
}

bool AskToPlayAgain()
{
cout << "Type yes to play again or no to exit: ";
string Response = "";
getline(cin, Response);

constexpr int Number_One = 1;
constexpr int Number_Zero = 0;

int  Answer_One = 1;
int Answer_Zero = 0;

string True = "Yes";
string False = "No";

string the_answer_1 = "";
string the_answer_2 = "";

// This is the divider I am not done with this shit.... 

Response[0] == 'y' || 'Y';
Response[1] == 'n' || 'N';

Response[0] = Number_One;
Response[1] = Number_Zero;

Response[0] = Answer_One;
Response[1] = Answer_Zero;

Answer_One = Number_One;
Answer_Zero = Number_Zero;

the_answer_1 = Response[0];
the_answer_2 = Response[1];

the_answer_1 = True;
the_answer_2 = False;

Response = the_answer_1;
Response = the_answer_2;

cout << (Response);
cout << endl;
return false;
}

1 个答案:

答案 0 :(得分:1)

首先响应[0]是字符串“Response”的第一个字符,Response [1]是字符串的第二个字符,依此类推。如果您键入“Cat”,则Response [0]将为“C”,Response [1]将为“a”,而Response [2]将为“t”。索引是从字符串开头的偏移量。请注意,这是字符串如何工作的基本内容。

在你的“bool AskToPlayAgain()”函数中基本上所有是乱码,实际上并没有做任何有意义的事情。

在使用谓词逻辑时,请注意匹配多个可能需要显式的可能性。你不能写,例如“天空是蓝色或灰色”,你需要写“天空是蓝色的,天空是灰色的”。作为逻辑/数学运算符的“OR”是在单个子句之后计算的,因此它们需要在OR运算符的任一侧独立地验证为真/假。

以下是如何完成的示例:

bool AskToPlayAgain()
{
    bool chosenResponse = false; // doesn't matter what this starts as
    bool correctInput = false; // set to true when we know the input was an allowed one

    while(correctInput == false) // keep looping until we get a good one
    {
        cout << "Type (y)es to play again or (n)o to exit: ";
        string Response;
        getline(cin, Response);

        // you want to compare the first character to n,N, y or Y
        // "Response[0]" is the first character of the input

        if(Response[0] == 'y' || Response[0] == 'Y')
        { 
            chosenResponse = true;
            correctInput = true;
        }
        else if(Response[0] == 'n' || Response[0] == 'N')
        { 
            chosenResponse = false;
            correctInput = true;
        }
        else
        {
            cout << "I didn't quite get that, can you try again ...\n\n";
        }
    } // here it loops back if the input didn't match the allowed ones

    return chosenResponse; // if we got out of the loop we know the user typed something beginning with y or Y, or n or N, which is good enough
}