【问题标题】:Question and Answer program crashes on release build, works fine on debug build问答程序在发布版本时崩溃,在调试版本中运行良好
【发布时间】:2019-11-09 19:22:54
【问题描述】:

我制作了一个包含 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;
    }
}

【问题讨论】:

  • 这里的代码不完整,无法编译。崩溃发生在哪里?
  • 这似乎发生在 ``` if ((ANSWER3 == "E") || (ANSWER3 == "e") || (ANSWER3 == "E)") || (ANSWER3 == "e)")) ``` 如果需要,我会输入整个代码。

标签: c++ crash


【解决方案1】:

好的,我知道问题出在哪里了。

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 后,程序将关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2011-12-22
    • 2013-04-30
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    相关资源
    最近更新 更多