问与答程序在发行版本上崩溃,在调试版本上正常运行

我制作了一个包含3个问题的问答程序(控制台应用程序),它在CodeBlocks 17.12(调试版本)中可以正常工作,但是在发行版本(.exe文件)中,它可以在正确回答时崩溃而没有任何错误第三个问题。输入正确答案后,程序不会崩溃。

我尝试使用WinDbg Preview进行调试,但大多数情况下它无法正常工作,并且在正确回答第三个问题并正常运行时,程序也不会崩溃。 我也尝试删除SetConsoleAttribute颜色,但这也不起作用。

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

zhujun1918 回答:问与答程序在发行版本上崩溃,在调试版本上正常运行

好的,我弄清楚了问题所在。

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

在这部分代码中,我忘记添加

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

现在就这样

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

因此,该程序不仅会关闭,而且在输入答案后会显示上面的文本。按ENTER键后,程序将关闭。

本文链接:https://www.f2er.com/3131573.html

大家都在问