1

I'm trying to follow a C++ class to get more acquainted with the language and Unreal Engine. However, now whenever I start the program, the console window immediately closes. I've already tried fixing the subsystem stuff and I've seen some dirty code that wouldn't close the window, but I'd like to know how to get the problem fixed without trying to find a weird circumnavigation.

/* This is the console executable, that makes use of the BullCowClass
This acts as the view in a MVC pattern, and is responsible for all 
user interaction. For game logic see the FBullCow`enter code here`Game class.
*/

#include <iostream>
#include <string>
#include "FBullCowGame.h"

using FText = std::string;
using int32 = int;

void PrintIntro();
void PlayGame();
FText GetValidGuess();
bool AskToPlayAgain();


FBullCowGame BCGame; //instantiatiate a new game
// entry point for our application

int main() 
{


    bool bPlayAgain = false;
    do 
    {
        PrintIntro();
        PlayGame();
        bPlayAgain = AskToPlayAgain();
    } while (bPlayAgain == true);



    return 0;
}

void PrintIntro() 
{
    //introduce to game

    std::cout << "Welcome to Bulls and Cows" << std::endl;
    std::cout << "Can you guess the " << BCGame.GetHiddenWordLength() << " letter isogram I'm thinking of?\n";
    std::cout << std::endl;
    return;
}
void PlayGame() 
{
    BCGame.Reset();
    int32 MaxTries = BCGame.GetMaxTries();

    //looping for the numbert of turns asking for guesses
    for (int32 i = 1; i <= MaxTries; i++)   {// TODO change from FOR to WHILE
        FText Guess = GetValidGuess();


        // submit valid guess to the game and receive counts
        FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);

        std::cout << "Bulls = " << BullCowCount.Bulls;
        std::cout << ". Cows = " << BullCowCount.Cows << "." << "\n\n";
    }

    // TODO summarize game here
}

//loop continually until the use gives a valid guess
FText GetValidGuess() // TODO change to GetValidGuess
{   
    EGuessStatus Status = EGuessStatus::Invalid_Status;
    do {
        //get a guess from the player
        int32 CurrentTry = BCGame.GetCurrentTry();

        std::cout << "Try " << CurrentTry << " What is your word guess: ";
        FText Guess = "";
        std::getline(std::cin, Guess);

         Status = BCGame.CheckGuessValidity(Guess);


        switch (Status)
        {
        case EGuessStatus::Wrong_Length:
            std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
            break;
        case EGuessStatus::Not_Isogram:
            std::cout << "Please enter an isogram.\n";
            break;
        case EGuessStatus::Not_Lowercase:
            std::cout << "Please enter your guess in lowercase letters.\n";
            break;
        default:
            return Guess;
        }
        std::cout << std::endl;
        {

        };
    } while (Status != EGuessStatus::OK); //keep looping until we get no errors

}
bool AskToPlayAgain() 
{
    std::cout << "Do you want to play again(y/n)? ";
    FText Response = "";
    std::getline(std::cin, Response);
    if (Response[0] == 'y') 
    {
        return true;
    }
    if (Response[0] == 'n') 
    {
        return false;
    }
    return false;
}
  • 1
    Did you try stepping through your code with a debugger? – Algirdas Preidžius Feb 19 '18 at 23:35
  • By "subsystem stuff" do you mean you already tried [this answer](https://stackoverflow.com/q/1775865/212858)? Or something else? – Useless Feb 19 '18 at 23:35
  • @Useless Yeah, I've been running my code with CTRL + F5 so that it wouldn't close abruptly at first. However, I don't see why it would close now seeing how I have code that gets user input and all. – Giovanni Vazquez Feb 19 '18 at 23:43
  • @AlgirdasPreidžius It works when I run through with the debugger but I'm trying to run it without the small paces of the debugger. – Giovanni Vazquez Feb 19 '18 at 23:43
  • @GiovanniVazquez "_It works when I run through with the debugger_" Probably my question wasn't clear enough, let me repeat it, while emphasizing a key phrase in the question: Did you try **stepping through** your code with a debugger? – Algirdas Preidžius Feb 19 '18 at 23:47
  • @AlgirdasPreidžius I stepped through with my debugger but there aren't any problems that I can see. The program runs fine for the most part with the debugger. But whenever I run it without debugging it still closes. – Giovanni Vazquez Feb 20 '18 at 00:05
  • in `GetValidGuess`, `while (Status != EGuessStatus::OK);` allows the loop to exit. There is no `return` statement outside of the loop. The compiler is now allowed to generate a program that does whatever it wants, from something that seems correct to the summoning of dread C'thulhu. Can't test that this is your bug, though, because there is no [mcve]. – user4581301 Feb 20 '18 at 00:12
  • @GiovanniVazquez You are meant to step through the code, with a debugger, line by line, while investigating the values of variables at each step. If all the values, at each step, are the ones that you expect - then then the code works as you expect it to - there is no problem. – Algirdas Preidžius Feb 20 '18 at 09:23

1 Answers1

0

IF you are redirecting your output to a text file then it wont stop after execution. You can check the projectsettings under Debugging Section.