【问题标题】:(C++) Console window keeps immediately closing even though I'm CTRL + F5 in Visual Studio(C++) 即使我在 Visual Studio 中按 CTRL + F5,控制台窗口也会立即关闭
【发布时间】:2018-07-30 06:04:16
【问题描述】:

我正在尝试学习 C++ 课程以更熟悉该语言和虚幻引擎。但是,现在每当我启动程序时,控制台窗口都会立即关闭。我已经尝试修复子系统的东西,并且我看到了一些不会关闭窗口的脏代码,但我想知道如何在不尝试寻找奇怪的环游的情况下解决问题。

/* 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;
}

【问题讨论】:

  • 您是否尝试过使用调试器单步执行您的代码?
  • “子系统的东西”是指您已经尝试过this answer 吗?还是别的什么?
  • @Useless 是的,我一直在用 CTRL + F5 运行我的代码,这样一开始它就不会突然关闭。但是,我不明白为什么它现在会关闭,看看我是如何获得用户输入的代码的。
  • @AlgirdasPreidžius 当我使用调试器运行时它可以工作,但我试图在没有调试器的小速度的情况下运行它。
  • @GiovanniVazquez "当我使用调试器运行时它可以工作" 可能我的问题不够清楚,让我重复一遍,同时强调问题中的关键短语:您是否尝试过使用调试器单步执行您的代码?

标签: c++ visual-studio unreal-engine4


【解决方案1】:

如果您将输出重定向到文本文件,那么它在执行后不会停止。您可以检查调试部分下的项目设置。

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多