1

I have the same problem as mentioned in the linked question. The console window (in VS 2010) disappears immediately after running the program. I use a cin.get(); at the end of the main function, but the problem still remains. Any idea about the possible reason? You can check out the code in main:

int main()
{
    const int arraysize = 10;
    int order;
    int counter;
    int a[arraysize] = {2,6,4,45,32,12,7,33,23,98};

    cout<<"Enter 1 to sort in ascending order\n"
        <<"Enter 2 to sort in descending order\n";
    cin>>order;
    cout<<"Data items in original order\n";

    for(counter=0;counter<arraysize;counter++){
        cout<<setw(4)<<a[counter];
    }

    switch (order){
        case 1: cout<<"\nData items in ascending order\n";
                selectionSort(a, arraysize, ascending);
                break;
        case 2: cout<<"\nData items in descending order\n";
                selectionSort(a, arraysize, descending);
                break;
        default: return 0;
    }

    for(counter=0;counter<arraysize;counter++){
        cout<<setw(4)<<a[counter];
    }

    cout<<endl;
    cin.get();

    return 0;
}

link : C++ on Windows - the console window just flashes and disappears. What's going on?

Community
  • 1
  • 1
arjacsoh
  • 8,002
  • 23
  • 99
  • 158
  • Have you run this in the debugger to check that a) it's getting to the `cin.get()` line and b) that it waits at that line for input? – ChrisF Nov 16 '11 at 12:20
  • 2
    What if `default: return 0;` is being hit? – sharptooth Nov 16 '11 at 12:21
  • I have made debugging and it seems not to wait for an input. ChrisF you are correct. But I cannot still figure out the reason. – arjacsoh Nov 16 '11 at 12:25
  • In that case step through **the entire program** on the debugger to see why not. I suspect @sharptooth is correct. – ChrisF Nov 16 '11 at 12:30
  • Sharptooth is correct, but if order is 1 or 2 the behavior is still reproduced. I updated my answer. – Luchian Grigore Nov 16 '11 at 12:34
  • I took the liberty of modifying the title in order to be easier to find by others experiencing the same problem. – Luchian Grigore Nov 16 '11 at 12:41
  • As an aside, if you want the console window to persist until the user presses a button you can always run without the debugger in visual studio by pressing Ctrl-F5. – Munro Nov 16 '11 at 12:57

4 Answers4

3

So when using cin.get() after cin, you should always remember to add cin.ignore() between them .

cin>>order;
cin.ignore();
/* 
   other codes here
 */
cin.get();

It is mainly because the CIN will ignore the whitespace in the buffer, so after cin>>order, there is a "newline"(\n) in the buffer, then your cin.get just read that \n, then you program successfully executed and return. The cin.ignore() will ignore previous input in the buffer. This really help!

I am a student in China. Your question is the first one I can answer here. I once had the same trouble as you. I hope this helps you. Ignore my poor english and thank you.

Jack
  • 8,921
  • 4
  • 44
  • 73
Archeosudoerus
  • 1,091
  • 9
  • 23
2

My guess is that

default: return 0;

get executed.

EDIT:

You're right, that's not the issue. Read this.

The quick fix is:

cout<<endl;
cin.ignore(); // <---- ignore previous input in the buffer
cin.get();

But you might want to read the article for further info on the behavior.

Luchian Grigore
  • 236,802
  • 53
  • 428
  • 594
  • Yes, that works and the window remains. The problem occurred as well when 1 or 2 is pressed. The default case made no difference. Thanks! – arjacsoh Nov 16 '11 at 12:36
  • id used cin.clear() instead because this resets the error flags, so that you wont get the previous cin.get()'s errors – QuantumKarl Nov 16 '11 at 17:08
  • `cin.ignore()` is important here. `clear` will indeed preserve error flags but OP needed to ignore previous input – Dan Bradbury Apr 01 '18 at 22:28
0

I bet you hit the default switch label (return 0;). This bypasses cin.get() - you need one cin.get() per return statement.

larsmoa
  • 11,450
  • 5
  • 54
  • 82
0

Probably your cin.get() is reading the newline which terminated your order input? You could try calling cin.get() twice.

Werner Henze
  • 15,279
  • 12
  • 41
  • 62