Hi all, ok i have just started learning C++ today and i have download "Visual C++ 2008 Express Edition" and i am trying to make the "Hello World" program. I clicked on "Create Project" and then "Win32 Console Application" and then typed in the name "Hello World" then i right clicked on "Source Files" then "Add New Item" and then ".cpp" and typed in "Hello World" then i wrote into the big black page...

#include <iostream>

using namespace std;

int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
  return 0;
}

And then i clicked on "Build" then "Build Solution" then i clicked the green arrow "Start Debugging"

Then ERROR... The black command prompt screen shows up for about 1 second and disapears before i can even read the writing.

Please help... Thanks for comments

you have to add something before the return 0 to make the program stop. like this:

#include <iostream>

using namespace std;

int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
  cin.get(); 
  return 0;
}

There are several other ways to do it too -- all of them are correct.

If you're on windows, a common way to do it is system("PAUSE"); . It gives a prompt like "Press any key to continue..."

This is a good forum :) Very fast responces and very helpful

If you're on windows, a common way to do it is system("PAUSE"); . It gives a prompt like "Press any key to continue..."

Why you should not use that.

Interesting, thank you for the advice, also i have another question, whenever you make a program in c++ visual 2008 does the program always appear in command prompt?

Interesting, thank you for the advice, also i have another question, whenever you make a program in c++ visual 2008 does the program always appear in command prompt?

No. Absolutely not. However (not 100% sure about this), I believe any time you have a main() function, a console will open in VC++. I ran this program:

int main()
{
}

and a console window still came up. I'm not sure if there's a way to prevent that. If you use WinMain() and create a window with the Windows API:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int nShowCmd)
{
}

a console won't come up.

Try putting this inbetween the parenthisis of the 'int main()'

int main(int argc, char*argv[])

and on a line inbetween 'return 0;' and the string, put...

system("PAUSE");

And that should help your problems.

also use getch();
include<conio.h>

Try putting this inbetween the parenthisis of the 'int main()'

int main(int argc, char*argv[])

and on a line inbetween 'return 0;' and the string, put...

system("PAUSE");

And that should help your problems.

Reasons to avoid system("PAUSE");

An easy way to do it is this....

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!" << endl << endl;
    system("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.