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?
So when using cin.get() after cin, you should always remember to add cin.ignore() between them .
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.
My guess is that
get executed.
EDIT:
You're right, that's not the issue. Read this.
The quick fix is:
But you might want to read the article for further info on the behavior.
I bet you hit the default switch label (
return 0;
). This bypassescin.get()
- you need onecin.get()
per return statement.Probably your cin.get() is reading the newline which terminated your order input? You could try calling cin.get() twice.