Archived

This topic is now archived and is closed to further replies.

JMgamer88

Hello.cpp

Recommended Posts

JMgamer88    122
Hello folk! I''m haven alittle bit of trouble with my first programm. I just downloaded a compiller and tried to write a hello world program. The program run for about a second to show hello but then goes away. can some one tell me home to get this to not happen. My code is as follows... #include int main() { cout<<"hello world!"; return 0; } is it my code? i appreciate you time in reading this and helping me. Thank you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I am of no help.

I also wrote one these programs and could find no way of pausing the program.

I know the keyword is: PAUSE in BASIC but i have no idea what it is in C++.

You may try a loop that runs until it detects a key stroke.

Share this post


Link to post
Share on other sites
DeusMaximus    122
Basically the compiler is running the program and then closing the window so fast that you can''t see what i happening. try running it from dos. that should work just fine.

Hope I helped,
Rob

Share this post


Link to post
Share on other sites
Phantasm    122
Okay, I think your problem is that it is running in a DOS Console...you're running a Windows machine, right?

Anyhow, I've run into this same problem using Borland Builder 4 at school. What you need to do is include conio.h. Before your return 0; statement, use getch(); Like this:



#include
#include

int main() {
cout << "hello world";

// getch() makes the program wait for the user to hit
// a key. It's use is more powerful than that, but for
// C++ programs, this is all you need to know for now.
getch();
return 0;
} // end main()



Try that...it should work.

Oh, BTW, Borland Builder 4 is pure evil. It's almost Visual Basic for C/C++!

P h a n t a s m -- "Through dreams I influence mankind."

Edited by - Phantasm on October 2, 2000 10:18:46 PM

Share this post


Link to post
Share on other sites
Cel    122
The html in this forum strips the angle brackets from messages,

the include libraries are

#include iostream.h
#include conio.h

just put em in angle brackets and you should be right

Cel

Share this post


Link to post
Share on other sites
Peon    276
When I made this program, I made a new c++ source file, wrote code, and compiled. The following worked fine:

#include

void main()
{
cout<<"Hello World"<}

I dunno why it won''t work above.

Share this post


Link to post
Share on other sites
Opticon    122
The reason the above one wouldn't work, is because you used int as the return type, then instructed main to return(0); This says that the program is over and done with, and closes the window out. The new program you wrote specifies that main's return type is void, and you don't return anything so the window stays open. The code is:

#include iostream.h //remember to include angle brackets!

void main(void)
{
cout << "Hello World";
}

This program ends, and doesn't do anything else, but it doesn't close the window (I'm pretty sure). The use of the getch() function waits for a keypress to end the program, then you have to close the window manually. But returning zero closes it out automatically. I don't think it has anything to do with which compiler you use.

















Edited by - Opticon on October 3, 2000 12:47:27 PM

Share this post


Link to post
Share on other sites
maketty    122
Well you could use a basic system function I dunno if it works with your compiler it should! (I''m using Dev C++ 4.0)

#include

int main()
{
cout << "Text goes here";

return 0;
// Right here is the system function
system ("pause")
}
// I hope that works I''m sorry for any errors I may have missed
// but you get the idea right? = )
// Goodluck



Matthew Fitzgerald (Maketty),
Knightvision Games

Share this post


Link to post
Share on other sites
maketty    122
Whoops the include preprocessor directive was

iostream.h

Also after the system pause you need a ; (Parse)

Sorry bout that!

Matthew Fitzgerald (Maketty),
Knightvision Games

Share this post


Link to post
Share on other sites
maketty    122
HOLY CRAP!

Ok now this is getting out of hand sorry!

Also the return statement should be after the system command!
Jeez I need to try write simple programs better I''m looking
like an idiot!

Matthew Fitzgerald (Maketty),
Knightvision Games

Share this post


Link to post
Share on other sites
kris vanhoof    122
Opticon: have you actually tried that out?

I doubt it will make a difference. The program will be terminated when it reaches the ending bracket.

Main is supposed to return 0 to the OS if the program was successfull, and some errorlevel if not. (you can use the errorlevels in batchfiles)

If you don''t return a value, it defaults to an integer anyway and it''ll return garbage.

Share this post


Link to post
Share on other sites
Opticon    122
kris vanhoof,
I tried it in my old compiler, and it works. You don''t really have to tell main to return anything (even though it may behind your back)... I admit the program does end, but the window stays open and you can see what the program ended up doing, you just can''t do anymore with it.

Share this post


Link to post
Share on other sites