3

I installed minGW and the eclipse CDT, and the console keeps doing something weird. The code of the program is

using namespace std;
#include <iostream>

int main() {
    cout << "Hello, windows (8, c++)" << endl;
    //system("PAUSE");
    return 0;
}

You all know it, its the Hello World program. Now when I run this the Eclipse console displays some stuff about building, and then goes blank. And when I navigate to the HelloWorldProgram.exe in the explorer and run it, a windows flashes up and displays "hello world", but then immediately closes. When I do this on Mac OSX there's no problem, and the windows stays up until I decide to close it. Now I know there's a command

system("PAUSE") //I dont know what I need to import to use this. Could you tell me that too?

Which will give me more or less the same effect, but I'd like to know why Windows does it differently from OSX, and what I can do to fix it (bc this annoys the crap out of me). Looking forward to your replies!

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
pipsqueaker117
  • 2,280
  • 9
  • 35
  • 47
  • Windows does it differently from OSX because it's not the same as OSX. It's an arbitrary design decision made by Microsoft and it's been the same for so many versions of Windows that it is unlikely to ever change. – Mark Ransom Dec 26 '12 at 18:43
  • @MarkRansom: ..by Microsoft... _and Linux_? Why _would_ an OS allocate a console to run a program? – xtofl Dec 26 '12 at 18:58
  • @xtofl, when you double-click on a console program there are two possible correct outcomes - display an error saying you can only run this from a console, or open a new console to run it in. Opening a new console is clearly more convenient. I really don't understand why they don't keep it open in that case, it annoys me too. – Mark Ransom Dec 26 '12 at 19:02

4 Answers4

3

This happens on Windows because this is just the behavior of the Windows console. You'll have to open up the console manually and then running your program through the console you've opened if you don't want the window to close automatically once the program has executed.

You may want to take a look at these:

What is the Best Practice for Combating the Console Closing Issue?

https://superuser.com/questions/186562/how-can-i-keep-the-terminal-open

Community
  • 1
  • 1
Stegrex
  • 4,004
  • 1
  • 17
  • 19
  • shouldnt Eclipse have some way to stop it from happening though? I get the validity of your answer and I accept it, just want to know if Eclipse has any fix for it. – pipsqueaker117 Dec 26 '12 at 18:48
  • I found a couple of documents about how to setup Eclipse for the console, and I would try this out (no guarantees though): 1. External Tools 2. You should already have a configuration for running programs, most likely you have the location of the `cmd.exe` set in one of the fields. 3. Add ` /K` at the end of the path. The /K command prevents the window from closing automatically. I'm not sure if this will work, but I know that /K works by itself, and it appears that you can set the path to the `cmd.exe` program. Please let us know if it works! – Stegrex Dec 26 '12 at 20:55
  • Wait, what? External tools? Is this within eclipse? – pipsqueaker117 Dec 27 '12 at 02:32
  • @pipsqueaker117 Here's some more documentation about it: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-exttools-running.htm&cp=0_3_12_4 Unfortunately, I don't run Eclipse so I can't help you there, but it would be a good thing to try to see if this solves your problem. – Stegrex Dec 27 '12 at 03:30
1

Don't use system("pause"), it's wrong for a multitude of reasons (read more about it here).

Put cin.get() before return and the window will stay open until you press enter.

Community
  • 1
  • 1
Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • I dislike changing program logic for something platform-specific. The change will need you to press enter twice on Mac. – xtofl Dec 26 '12 at 18:54
  • I wouldn't call this "changing the program logic". The main thing is the OP can use the same code to see the results of his program on the both operating systems, not depending on the IDE's settings and quirks. – Gabriel Negut Dec 26 '12 at 19:00
1

If you want to just run your console program, you should open a console, and run it.

Apparently, the OSX version of Eclipse is configured to open a console, and run the program, and not close it. Maybe you can configure the Win version so, too.

You shouldn't meddle with your program to behave differently on another platform, instead wrap it into something that 'adapts' the behaviour.

Probably, you can tell eclipse to use "cmd /c 'yourprogram.exe && pause'", to open a command window and have it execute your program and then pause.

xtofl
  • 40,723
  • 12
  • 105
  • 192
0

Just add getch(); before return, and add #include <conio.h>.

Khalif21
  • 97
  • 2
  • 11