本地Windows调试器不断崩溃

时间:2020-11-06 15:30:14

标签: c++ visual-studio

在尝试学习C ++时,我在Visual Studio上遇到本地调试器的问题,一旦到达代码末尾,它将立即关闭,并且不允许我自己关闭它,因此我可以甚至看不到输出的最后一位。

据我所知,我的代码中没有错误,我尝试了getchar(),以便可以在调试器关闭之前输入一个字符。

这是我的代码,以防万一有错误,但我不认为有

    #include <iostream>

    using namespace std;

    int search(char* pchs, int size, char key) {
    int count = 0;

    for (int i = 0; i < size; i++) {
        if (pchs[i] == key) 
            count++;    
    }
    return count;
    }

    int main() {
    int size = 0;
    char key = 0;
    cout << "Please enter the size of the array" << endl;
    cin >> size;
    cout << "Please enter the key (a-z)" << endl;
    cin >> key;

    char* pchs = new char[size];

    if (pchs != NULL) {
        for (int i = 0; i < size; i++) {
            pchs[i] = 97 + rand() % 26;
            cout << pchs[i] << " ";
        }
        cout << endl;
        cout << "No. Occurences: " << search(pchs, size, key) << endl;
    }
    
    
    delete[] pchs;
    return 0;
}

2 个答案:

答案 0 :(得分:3)

转到工具->选项,然后搜索并禁用该选项 Automatically close the console when debugging stops

答案 1 :(得分:0)

您可以尝试此方法:

右键单击您的项目

属性->配置属性->链接器->系统->选择控制台(/ SUBSYSTEM:CONSOLE)