Answered by:
Why does my console open twice when i run?

Question
-
Ok guys, i created a new C++ empty project, added a source file (source.cpp), typed in a simple code such as "Hello World". And when i compile using F5, well, the console opens with my Hello World, but after i close it, the console will always pop up again a second time doing the exact same thing (Hello World). Does anyone have any idea what is going on?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World." << endl;
system ("pause");
}Monday, March 24, 2014 8:20 AM
Answers
-
>Removed system("pause") and changed to cin.get() instead, still the same problem.
Weird!
What happens if you use F10 to step through the program with the
debugger?What happens if you use Ctrl+F5?
Dave
Well.. Ctrl + F5 did the same thing, opened and run twice.
When i press F10, the console open once, and my program run as per normal, and then my program ended, the console closes, and the 2nd console will open which will be blank, with a yellow arrow beside the my 'int main(){'.
LOLOLOLOLOL!!! Solved............ So sorry so wasting all ur time.. I feel so damn bloody stupid right now.. The problem that caused this was................. "Avast Deepscreen" zZzZzZzZz....
Apparently the 1st window that pops up was avast deepscreen.. -_- I disabled it and it's fine now.. X_X Thank you all for all ur time n effort.. >_<
- Proposed as answer by WayneAKing Friday, March 28, 2014 6:26 AM
- Marked as answer by May Wang - MSFT Tuesday, April 1, 2014 1:57 AM
Friday, March 28, 2014 6:04 AM
All replies
-
Ok guys, i created a new C++ empty project, added a source file (source.cpp), typed in a simple code such as "Hello World". And when i compile using F5, well, the console opens with my Hello World, but after i close it, the console will always pop up again a second time doing the exact same thing (Hello World). Does anyone have any idea what is going on?
system ("pause");
Remove the call to system. It's invoking an instance of the command
processor.Dave
Monday, March 24, 2014 9:35 AM -
Ok guys, i created a new C++ empty project, added a source file (source.cpp), typed in a simple code such as "Hello World". And when i compile using F5, well, the console opens with my Hello World, but after i close it, the console will always pop up again a second time doing the exact same thing (Hello World). Does anyone have any idea what is going on?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World." << endl;
system ("pause");
}David Wilkinson | Visual C++ MVP
Monday, March 24, 2014 9:48 AM -
Removed system("pause") and changed to cin.get() instead, still the same problem.Wednesday, March 26, 2014 1:47 AM
-
Ok guys, i created a new C++ empty project, added a source file (source.cpp), typed in a simple code such as "Hello World". And when i compile using F5, well, the console opens with my Hello World, but after i close it, the console will always pop up again a second time doing the exact same thing (Hello World). Does anyone have any idea what is going on?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World." << endl;
system ("pause");
} This does not happen to me. Which version of Visual C++ are you using?
David Wilkinson | Visual C++ MVP
Wednesday, March 26, 2014 1:48 AM -
-
>Removed system("pause") and changed to cin.get() instead, still the same problem.
Weird!
What happens if you use F10 to step through the program with the
debugger?What happens if you use Ctrl+F5?
Dave
Well.. Ctrl + F5 did the same thing, opened and run twice.
When i press F10, the console open once, and my program run as per normal, and then my program ended, the console closes, and the 2nd console will open which will be blank, with a yellow arrow beside the my 'int main(){'.
Friday, March 28, 2014 4:56 AM -
Hi Lancerdragon,
As it is an OS feature Its really not a recommended way to use System("pause").
Its really a time consuming task.
1)suspend your program
2)call the operating system
3)open an operating system shell (relaunches the O/S in a sub-process)
the O/S must now find the PAUSE command
4)allocate the memory to execute the command
5)execute the command and wait for a keystroke
6)deallocate the memory
7)exit the OS
8)resume your program
The language support much better way : include stdlib.h or cstdlib So in C, use getchar() instead. In C++, how about cin.get().
Thanks,
BhashFriday, March 28, 2014 5:17 AM -
Hi Lancerdragon,
As it is an OS feature Its really not a recommended way to use System("pause").
Its really a time consuming task.
1)suspend your program
2)call the operating system
3)open an operating system shell (relaunches the O/S in a sub-process)
the O/S must now find the PAUSE command
4)allocate the memory to execute the command
5)execute the command and wait for a keystroke
6)deallocate the memory
7)exit the OS
8)resume your program
The language support much better way : include stdlib.h or cstdlib So in C, use getchar() instead. In C++, how about cin.get().
Thanks,
BhashHi Bhash,
Thank you for the detailed explanation. I shall change it to cin.get() then.
But still it doesn't solve the issue i am facing. I have to run my program and see it on the 1st console, then when im done i still have to close the 2nd console that pops up after i close the 1st console.
Friday, March 28, 2014 5:21 AM -
>Removed system("pause") and changed to cin.get() instead, still the same problem.
Weird!
What happens if you use F10 to step through the program with the
debugger?What happens if you use Ctrl+F5?
Dave
Well.. Ctrl + F5 did the same thing, opened and run twice.
When i press F10, the console open once, and my program run as per normal, and then my program ended, the console closes, and the 2nd console will open which will be blank, with a yellow arrow beside the my 'int main(){'.
LOLOLOLOLOL!!! Solved............ So sorry so wasting all ur time.. I feel so damn bloody stupid right now.. The problem that caused this was................. "Avast Deepscreen" zZzZzZzZz....
Apparently the 1st window that pops up was avast deepscreen.. -_- I disabled it and it's fine now.. X_X Thank you all for all ur time n effort.. >_<
- Proposed as answer by WayneAKing Friday, March 28, 2014 6:26 AM
- Marked as answer by May Wang - MSFT Tuesday, April 1, 2014 1:57 AM
Friday, March 28, 2014 6:04 AM -
The problem that caused this was................. "Avast Deepscreen"
Ah yes ... automatic "sandboxing" to test suspicious/unknown programs before letting them run "live".
>I disabled it and it's fine now..
If you mean you disabled "Deepscreen" then a better/safer method would be to make your
directories where the program's you're building reside "exclusions" in avast!'s settings.
That way you won't lose the protection completely.
- Wayne
Friday, March 28, 2014 6:26 AM -
As it is an OS feature Its really not a recommended way to use System("pause").
<off topic start>
It's system("pause"); - we're using a case-sensitive language.
It's only a concern if anticipating porting code to another platform which may not support
the "pause" operation. The system() function is part of ANSI C and so is supported by all
Standards-conforming compilers. Even this porting concern is trivial so long as this usage
is limited to program termination. Then it can be changed easily enough - usually only a
single line change - if "pause" isn't supported on the target platform. Putting it in an
atexit() function allows system("pause") to be specified only once regardless of the number
of exit/return points in main().
>Its really a time consuming task.
You make it sound like that, and I'm sure a profiler may agree. But realistically we're
talking about milliseconds here - usually imperceptible to a human. I would love to sit
you down before a computer and have you execute two versions of a test program - one using
system("pause") and the other using getchar(), getch(), cin, etc. I'd wager that you would
not be able to tell me which is which.
Given that what the end-user (programmer) is waiting for here is program execution suspension,
typically program output to the console is already on the screen and whether the user is
waiting for system("pause") to pause the execution or some other method the result is the
desired effect: the console stays open and screen contents can be examined. Whether this
occurs before, during or after the execution of the chosen method is irrelevant - the
desired effect is achieved in any case and any "performance" issues with the method used
are transparent. It's not as if the user is waiting anxiously for the pause to complete so
he/she can resume execution immediately.
>The language support much better way ...
"Better" is a subjective assessment. "Much better" is hyperbole.
In the final analysis, obsessing over such minutiae is counter-productive.
Let the coder exercise his/her prerogative and use whichever method is most
comfortable. If it's not going to be left in the Release build, then arguably
a better method is to use debugger breakpoints. IMHO
The same arguments that you advance against system("pause") could be suggested in
opposition to system("cls") but I've never seen a programmer complain that this
takes too long to clear the screen and resume program execution. In real life
there is rarely if ever a performance issue with using system() - I don't even
recall any when using these methods on a 10 MHZ XT.
<off topic end>
- Wayne
Friday, March 28, 2014 6:32 AM -
<g class="gr_ gr_33 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" data-gr-id="33" id="33">Dude</g> I'm pretty sure you just saved me many hours of pulling my hair out. Had the same issue with avast ;p
Thursday, May 24, 2018 1:20 PM