C++ 控制台应用程序,输出不正确的数据

C++ Console Application, Outputting Incorrect Data

我一直在开发一个简单的控制台应用程序,但在编译我的最新代码时,它开始输出与我输入的内容不匹配的文本和整数字符串。

到目前为止,该程序的目的很简单:输入一串数据并使其在控制台应用程序中多次正确输出。下面我链接了伪代码。

提前致谢。

#include <iostream>
#include <string>

void printIntro();
void RunApp();
bool RequestRestart();

std::string GetAttempt();

int main() // entry point of the application 
{
    printIntro();
    RunApp();
    RequestRestart();

    return 0;
}

void printIntro() {

    // introduce the program
    constexpr int WORD_LENGTH = 8; // constant expression

    std::cout << "Welcome to the Bull and Cow guessing game\n";
    std::cout << "Can you guess the " << WORD_LENGTH;
    std::cout << " letter isogram I am thinking of?\n\n";

    return;
}

void RunApp()
{
    // loop for number of attempts
    constexpr int ATTEMPTS = 5;
    for (int count = 1; count <= ATTEMPTS; count++)
    {
        std::string Attempt = GetAttempt();

        std::cout << "You have entered " << GetAttempt << "\n";
        std::cout << std::endl;
    }
}

std::string GetAttempt() 
{
    // receive input by player
    std::cout << "Enter your guess: \n";
    std::string InputAttempt = "";
    std::getline(std::cin, InputAttempt);

    return InputAttempt;
}

bool RequestRestart() 
{
    std::cout << "Would you like to play again?\n";
    std::string Response = "";
    std::getline(std::cin, Response);

    std::cout << "Is it y?: \n" << (Response[0] == 'y'); //response must be in brackets

    return false;
}

您正在打印指向 GetAttempt 的指针。而是打印 Attempt:-

std::cout << "You have entered " << Attempt << "\n";

你必须改变这一行 std::cout << "You have entered " << GetAttempt << "\n";std::cout << "You have entered " << Attempt << "\n";

通过这种方式,您不会像以前那样打印函数的地址,而是打印存储 GetAttempt 函数的 return 值的变量。