"Hello World" ?!?!?!?!

Status
Not open for further replies.
pcrobot said:
OK, I type this:
// my first program in C++

#include <iostream.h>
#include <cstdlib>

int main ()
{
cout << "Hello World!";
return 0;
system("PAUSE");
}
And still it doesn't work... :(
Emily said:
Try putting it before return 0;
pcrobot said:
It works!
*smacks forehead*

if (succeed = true){
cout << "Good Job";
}
else {
cout << "Read Programming 101";
}
 
pcrobot,

You shouldn't need to put the system pause inside of your program (at least not for a "Hello World"). Check to see if your compiler program has a "Start Without Debugging" option. I use Microsoft Visual Studio.NET 2003 as my compiler program and it has this option (not sure if other compilers do or not). If you choose to debug the program under VS.NET it does the same thing that you described earlier about opening the command prompt, running the program, and then closing the command prompt before you have any idea as to whether it worked or not.

If you choose to "Start Without Debugging", it runs your program to the end but then waits for the user to "Press any key to continue" before it closes. For me this is CTRL+F5.

As I am new to programming altogether, I know nothing about variations between different compiler programs. So I'm sorry if this doesn't help you.
 
The problem is just in the way he's executing his code.

If he actually opens up the command prompt, THEN runs the code, the command prompt will stay up. If he just runs his code (clicks the exe or whatever) then Windows grabs the file, executes it, and then terminates when it needs to. Which is what it is doing. It brings up the command prompt to throw out the user text echos, and then since the program has nothing else to do, closes the command prompt.

It's just the way Windows works. This isn't a fault in his code.

He can put a loop or pause or whatever in his code to hold things, which means Windows doesn't see that the program ended and thus the command prompt stays open. But that's just a work-around for an execution problem, not a code problem.

If he doesn't want the window to close right away, he should run the code properly.
 
I also use bloodshed C++, and started having the same problem. As has been stated, windows runs the program and closes because the program had executed completely. If the program pauses for input at the end of the program, the window will remain open. So, you can declare a variable and use cin to 'ask' for a value. For example:

#include <iostream.h>

int main
{
cout << "Main body of program";
char response;
cin >> response; //The program will pause here.
return 0;
}
 
hey! just a getch(); command will do! try this! add the library file conio.h
#include <conio.h>
and add the line

getch();

at the end of code.It will wait for a keyboard input.So until u press a key the program will show the prompt.


// my first program in C++

#include <iostream.h>
#include <conio.h>

int main ()
{
cout << "Hello World!";
getch();
return 0;

}
 
I use Dev-C++ as well and it works fine for me. It's not quite exactly right how you wrote it.

#include <iostream>

int main()
{
std::cout << "Hello World!\n";
system("pause");
return 0;
}

If that doesn't work, do this:

int main()
{
std::cout << "Hello World!\n";

std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
return 0;
}

For the second one, that long line tells the DOS window to close only after you press the enter key once. If you put
std::cin.ignore(std::cin.rdbuf()->in_avail() + 2); then you'd need to hit enter twice.
 
Status
Not open for further replies.
Back
Top Bottom