简体   繁体   English

问答程序在发布版本时崩溃,在调试版本中运行良好

[英]Question and Answer program crashes on release build, works fine on debug build

I have made a question and answer program (Console application) with 3 questions, it works just fine in CodeBlocks 17.12 (debug build), but on the release build ( the.exe file ) it crashes without any error when correctly answering the 3rd question.我制作了一个包含 3 个问题的问答程序(控制台应用程序),它在 CodeBlocks 17.12(调试版本)中运行良好,但在发布版本(.exe 文件)中,它在正确回答第三个问题时崩溃而没有任何错误. The program doesn't crash when putting in the wrong answer.输入错误答案时程序不会崩溃。

I tried to debug with WinDbg Preview but most of the time it didn't work and when it did the program did NOT crash when correctly answering the 3rd question and worked as normal.我尝试使用 WinDbg Preview 进行调试,但大多数时候它不起作用,并且当它正确回答第三个问题并正常工作时,程序没有崩溃。 I also tried to remove the SetConsoleAttribute colors, but that didn't work either.我还尝试删除 SetConsoleAttribute colors,但这也不起作用。

#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <windows.h>

using namespace std;

int main()
{
SetConsoleTitle("Bloodrush: The Game");
cout << "Question 3" << endl;
cout << "text" << endl;
cout << "A)" << endl;
cout << "B)" << endl;
cout << "C)" << endl;
cout << "D)" << endl;
cout << "E)" << endl;
string ANSWER3;
cin >> ANSWER3;
if ((ANSWER3 == "E") || (ANSWER3 == "e") || (ANSWER3 == "e)") || (ANSWER3 == "e)"))
 {
   if (system("CLS"))
        system("clear");
    cout << "text" << endl;
    cout << "text" << endl;
    cout << "text \n" << endl;
    cout << "text" << endl;
    return 0;
  }
    else
    {
            SetConsoleTitle("Wrong Answer");
            if (system("CLS")) system("clear");
            cout << "text \n" << endl;
            cout << "Press ENTER to return to the MAIN MENU..." << endl;
            cin.ignore();
            cin.get();
            cout << "no menu" << endl;
    }
}

Ok, I figured out what the problem was.好的,我弄清楚了问题所在。

if ((ANSWER3 == "E") || (ANSWER3 == "e") || (ANSWER3 == "e)") || (ANSWER3 == "e)"))
 {
   if (system("CLS"))
        system("clear");
    cout << "text" << endl;
    cout << "text" << endl;
    cout << "text \n" << endl;
    cout << "text" << endl;
    return 0;
  }

In this part of the code I forgot to add在这部分代码中我忘了添加

cin.ignore(); // These 2 commands make it so that you need to press                
cin.get();    // ENTER to continue running the program

So it is now所以现在

if ((ANSWER3 == "E") || (ANSWER3 == "e") || (ANSWER3 == "e)") || (ANSWER3 == "e)"))
 {
   if (system("CLS"))
        system("clear");
    cout << "text" << endl;
    cout << "text" << endl;
    cout << "text \n" << endl;
    cout << "text" << endl;
    cin.ignore();
    cin.get();
    return 0;
  }

So now the program doesn't just shut off, it displays the text above after inputing the answer.所以现在程序不只是关闭,它会在输入答案后显示上面的文本。 After pressing ENTER the program will shut off.按下 ENTER 后,程序将关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM