6

I am having trouble executing my C++ code. I have written a basic "Hello World" program, and compiled it using the g++ make command. Here is my code:

#include <iostream>

using namespace std; 

int main() {
  cout << "Hello World" << endl;
  return 0;
}

I am on Windows 10, using Emacs for code editing, and CygWin for compilation. I saved this file as hello.cpp. I then navigated to the directory in CygWin. Then I did the command make hello. This created hello.exe. Then, I attempted to execute the file using ./hello.exe. I also tried ./hello which also didn't work. When I type one of these commands and hit Enter, it just on the next line, not doing anything. I can type in this blank line, but it won't do anything. Does anyone know a way to make my code execute properly. Thank you.

EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine.

Buddy
  • 10,874
  • 5
  • 41
  • 58
kungfushark
  • 166
  • 1
  • 8

2 Answers2

2

Your program probably is working but the console window is closing before you can see anything.

Try adding an input at the end of the program so it will wait.

I.E.

int a;
cin >> a;
Rabbi Shuki Gur
  • 1,656
  • 19
  • 36
  • 1
    The console window doesn't close. It stays open, but eventually I have to close it because it doesn't output anything. – kungfushark Jan 07 '16 at 00:14
0

Your code is most likely executing, but not outputting anything. That's because it's failing. Try checking the return value after it has run with echo $?. If it's not 0 then it has crashed. Also run it in gdb and see if it fails. The reason why it's failing is most likely a windows/cygwin clash - it's not your code.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • I ran `echo $?` to check the return value and it said `0`, however, every time I run the program I have to restart CygWin, so the value may not be accurate. I do not know how to run it in `gdb`, although I do have it installed. – kungfushark Jan 06 '16 at 02:43
  • Every time you restart the shell, you lose your old value of `$?`. And it sounds like even the shell is crashing when you run your program. – Paul Evans Jan 06 '16 at 05:53