我一直在研究一个简单的控制台应用程序,并且在编译我的最新代码时,它开始输出与我输入的内容不匹配的文本和整数字符串。
到目前为止,程序的目的很简单:输入一串数据并在控制台应用程序中多次正确输出。下面我链接了伪代码。
提前致谢。
#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;
}
答案 0 :(得分:0)
您正在打印指向GetAttempt
的指针。而是打印Attempt
: -
std::cout << "You have entered " << Attempt << "\n";
答案 1 :(得分:0)
您必须更改此行
std::cout << "You have entered " << GetAttempt << "\n";
在std::cout << "You have entered " << Attempt << "\n";
这样就不会像以前那样打印函数的地址,而是打印存储GetAttempt
函数返回值的变量。