JDL Wahaha
JDL Wahaha

Reputation: 715

Prevent Visual Studio C++ console exit immediately without system("pause") or extra get

I know there have been many work around proposed for immediate shutdown, but I was wondering if there is other way to do so for cross platform?

I think system("pause") is visual studio / windows specific and getchar() or other similar stuff that waits for users' input create unnecessary input for exiting the program running under say gcc.

Any idea?

-- Edit --
I also tried hitting Ctrl+F5, but it doesn't work sometimes. So I'm looking for an alternative command (if there's any) or setup that can pause the console screen in visual studio and doesn't cause any discrepancy in other c++ compilers.

Upvotes: 1

Views: 3483

Answers (2)

rasul.ramezanzade
rasul.ramezanzade

Reputation: 11

u can use this method after that u write the last code of your program(before return 0;) u can use for example ( cin >> x; ) command.. then the program will wait for u to enter new data. and u can see your answer in debugging program :D.. good luck with this trick :D

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182885

This problem only occurs when you launch a console program from a GUI. So there is a very simple cross-platform workaround -- run console programs from a console. If you want to make a program that runs well from a GUI, make a GUI program.

The other suggested workarounds are awful. Both getchar() and system("pause") interfere with any attempt to use the program as a filter or to redirect its input and output. It doesn't make sense to break a program so that it works "correctly" when used incorrectly.

Upvotes: 4

Related Questions