views:

128

answers:

3

I wrote some very simple code since I'm just starting C++ and I want to get warmed up with the syntax and compiler before our binary tree assignment.

#include <iostream>

using namespace std;

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

The only output I'm receiving is:

1> Build started: Project: First-BinaryTree, Configuration: Debug Win32 ------
1>Compiling...
1>First-BinaryTree.cpp
1>Build log was saved at "file://c:\Users\Administrator\Documents\Visual Studio 2008\Projects\First-BinaryTree\First-BinaryTree\Debug\BuildLog.htm"
1>First-BinaryTree - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

It appears to have run correctly, but I don't see Hello in the output anywhere.

+1  A: 

Right now you are not EXECUTING the program you are merely COMPILING it

Since you are using VS 2008 the hotkey to run a program is (usually) F5

It appears under menu : "Debug -> Start Debugging"

To view the output of your program it is in the "Output Window"

To make sure it is present to go menu : "Debug -> Windows -> Output"

The results you are see from compilation/linking (you don't really have to worry about the difference right now) are in the Output window as well.

Eric
A: 

I think you need an endl to flush the stream...

cout << "Hello" << endl;

Robert Karl
+1  A: 

It seems that you have just built the project, without starting it. If you want to start it, you have to go to Debug->Run. However, keep in mind that in that way the executable will be started, it'll run and its window will disappear in some fraction of second, since it does almost nothing. If you want to be able to see the output, you may:

  • place a breakpoint on the return 0 (so the debugger will pause the program before its end);
  • start the program without debugging (if I recall correctly you have to do CTRL+F5, however there's the related menu item in the Debug menu); VS.NET will add a "Press a key to exit" message before the end of the application, however the debugger won't be attached to your executable;
  • add just before the return the following code:

 

cout<<"Press Return to exit...";
cin.sync();
cin.ignore();

For the flushing thing that someone mentioned, I'm not sure if it's needed: at the end of the program the cout object is destroyed, so it should flush itself automatically (correct me if I am wrong).

Matteo Italia
I chose the breakpoint option. It worked. Thank you.
Azreal
Nice choice, it's almost always the best one. :)
Matteo Italia