0

I'm running into problems with my local debugger on visual studio while trying to learn C++, as soon as it gets to the end of the code it immediately closes and doesn't allow me to close it myself so I can't even see the final bit of output.

As far as I know there are no errors in my code and I tried the getchar() so that I could enter a character before the debugger closing.

Here's my code just incase there are errors but I don't think there are

    #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;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Cbrady
  • 93
  • 1
  • 2
  • 7
  • Run it with `ctrl-f5`. – 001 Nov 06 '20 at 15:32
  • Tried that, same problem occurs. As soon as i enter my 2 user inputs it just closes down straight away, no error message or anything with f5 and crtl f5 – Cbrady Nov 06 '20 at 15:34
  • Are you sure you are running the correct revision? I don't see anything glaringly wrong. It should at least get the part where it asks for a key. – 001 Nov 06 '20 at 15:39
  • @JohnnyMopp OP didn't say it didn't – Asteroids With Wings Nov 06 '20 at 15:39
  • It does get to the part where it asks for the key, however after user input thats where it just closes down straight away – Cbrady Nov 06 '20 at 15:40
  • @Cbrady _"after user input thats where it just closes down straight away"_ You don't actually know that; it could be completing the program and producing the output, just not keeping the window open long enough for you to see it. Don't make assumptions. There's also no evidence of any "crashing"... – Asteroids With Wings Nov 06 '20 at 15:41
  • Ok. I misread "As soon as i enter my 2 user inputs" as "as soon as I enter a 2". Either way, the code looks fine and should run to completion. https://ideone.com/P1ltmC – 001 Nov 06 '20 at 15:41
  • By the way, I think `key` will always be a newline... – Asteroids With Wings Nov 06 '20 at 15:41
  • Side note: you are not seeding the psuedo-random number generator so your code will produce the same sequence of letters each time. – 001 Nov 06 '20 at 15:43
  • Yeah i noticed that when i set a breakpoint, was always the same but thanks anyway!:) – Cbrady Nov 06 '20 at 15:44
  • 1
    Try putting `char c; cin >> c;` right before `return 0;`. – 001 Nov 06 '20 at 15:47
  • That seemed to work and it doesn't just close now.. not sure why this works lol – Cbrady Nov 06 '20 at 15:48
  • That just waits for the user the enter any character. The real question is why `ctrl-f5` does not work for you? Do you have one of those keyboards that re-maps the function keys? – 001 Nov 06 '20 at 15:51
  • Yeah but i tried the same with getchar() to get the user to enter a character before closing and it didnt work. No my crtl f5 runs without debugging, also clicked debug > run without debugging just to make sure – Cbrady Nov 06 '20 at 15:53

2 Answers2

3

Go to Tools -> Options and search for this option and disable it Automatically close the console when debugging stops

Lily
  • 1,386
  • 2
  • 13
  • 16
  • https://gyazo.com/342c71fb8c896f16c3b16baf475a29ce I don't seem to have that option on my Visual Studio 2017 – Cbrady Nov 06 '20 at 15:38
  • You are probably running an older version of Visual Studio then, but it probably is not that solution in that case. – Lily Nov 06 '20 at 15:40
  • I just remember it solving a similar problem for me in the past. – Lily Nov 06 '20 at 15:40
  • https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio tried solutions on this here but they dont seem to work either – Cbrady Nov 06 '20 at 15:44
  • @Cbrady Is your VS 2017 version [15.9.4](https://stackoverflow.com/a/54084067/5538420) or higher? If not you should Run / Check for Updates and bring your installation up to date. – dxiv Nov 09 '20 at 06:38
0

You could try this method:

Right click on your project

Properties -> Configuration Properties -> Linker -> System -> Select Console (/SUBSYSTEM:CONSOLE)

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7