Search code examples
c++outputvisual-studio-2017pseudocode

C++ Console Application, Outputting Incorrect Data


I've been working on a simple console application and was stopped when, upon compiling my latest code, it began outputting strings of text and integers which did not match what I have entered.

The purpose of the program thus far is simple: to input a string of data and for it to output correctly multiple times in the console application. Below I have linked the pseudocode.

Thanks in advance.

#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;
}

Solution

  • You have to change this line std::cout << "You have entered " << GetAttempt << "\n"; instd::cout << "You have entered " << Attempt << "\n";

    In this way you do not print the address of the function, just like you did before, but the variable in which you stored the return value of the GetAttempt function.