954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hello World

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

DonkeyKong92
Newbie Poster
20 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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

DonkeyKong92
Newbie Poster
20 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

DonkeyKong92
Newbie Poster
20 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 
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.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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.

Kungpao
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

also use getch();
include

aryansmit3754
Newbie Poster
4 posts since Jan 2009
Reputation Points: 9
Solved Threads: 0
 

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");

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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


#include
using namespace std;

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

Kungpao
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You