回答y或Y,n或N时的奇怪响应

时间:2018-06-15 05:22:26

标签: c++ xor

当我放置循环时,我想在不同的功能中放置一个cin并根据提供的bool返回。我以为我做对了,但它回归了一些奇怪的东西。它看起来不像下溢或溢出。它返回00DB1078,对我来说这看起来像一个十六进制。无论如何,我不确定我做错了什么或从哪里开始研究这类问题。这是我的代码。

    cout << "That was fun! Would you like to play again? ";
getline(cin, Response);
// cout << "Is it y? " << (Response[0] == 'y' || Response [0] =='Y');
if
    (Response[0] == 'y' || Response[0] == 'Y')
    return true;
else if (Response[0] == 'n' || Response[0] == 'N')
    return false;

这是它所要求的地方。

    int main()
{
// do while (Response == 1)
PrintIntro();
PlayGame();
AskToPlayAgain();
cout << "You said" << AskToPlayAgain;
return 0;
}

2 个答案:

答案 0 :(得分:1)

检查功能是否正常返回的调用存在缺陷。要在函数名的末尾修复place()。

答案 1 :(得分:0)

您的代码存在的问题是您打印函数AskToPlayAgain 的地址,而不是 function AskToPlayAgain 返回的值。

要更正此问题,请将行cout << "You said" << AskToPlayAgain;更改为cout << "You said" << AskToPlayAgain();

以下是更正后的代码。你可以see it working here

#include <iostream>
using namespace std;


bool AskToPlayAgain()
{
    string Response;
    cout << "That was fun! Would you like to play again? ";
    getline(cin, Response);
    // cout << "Is it y? " << (Response[0] == 'y' || Response [0] =='Y');
    if
        (Response[0] == 'y' || Response[0] == 'Y')
        return true;
    else if (Response[0] == 'n' || Response[0] == 'N')
        return false;
    //For other cases when 1st character is not 'n' or 'N' or 'y' or 'Y'
    return false;
}

int main() {
    // do while (Response == 1)
    //PrintIntro();
    //PlayGame();
    //AskToPlayAgain();
    cout << "You said " << AskToPlayAgain();
    return 0;
}