PDA

View Full Version : silly problem


H1234
08-08-2003, 03:49 PM
Hi, I've just started to learn C++, and I'm having a similar problem to seph up there. I typed the following code into dev version 4, and although it compiles and executes, it only does so for about half a second before it closes itself...does anyone know why?...it does this on both my desktop and laptop. Please don't spout jargon in any replys cos I'm only on, like, page 9 of the self teach book!!!!....here's the code

#include <iostream>
using namespace std; //introduces namespace std
int main()
{
cout << "Hello World" ;

return 0;
}

Thanx people

stuka
08-08-2003, 05:43 PM
I presume you're running this from a GUI (windows most likely). If so, get to a command prompt (Start->Run...type 'cmd' or 'command'). Then cd to the program directory and type its name there. You'll see the output on screen then.

sicarius
08-08-2003, 11:12 PM
another thing you might do is to put a call to the function: getch
into your code. You don't need to under stand why right now, but it will effectively make the window stay open until you hit a key.

so try:



#include <iostream>
using namespace std; //introduces namespace std
int main()
{
cout << "Hello World" ;
getch()
return 0;
}

H1234
08-09-2003, 04:45 PM
thanx for that lads..but...

I'm actually pretty dumb,if I get to the dev directory in DOS do I just type in the name of the cpp file, or the exe..I tried but I think I did it wrong.

Secondly, if I put the call to the getch function like the honourable Mr Sicarius suggested, I just get an error report on compilation...I don't know what's up...I'm just trying to learn this damn language, but it doesn't help if the stuff in the book doesn't work as it should do....grrrr.

jamessan
08-09-2003, 06:04 PM
It does work as it's supposed to. When you ran the program from your Integraged Development Environment (IDE), such as Visual Studio, the console pops up, displays Hello World (cout << "Hello World";) and exits (return 0;) just like your code tells it to. One way to get around that is to use the getch() (get character) command like sicarius suggested. The reason that your code would not compile when you added getch() was because you must #include <conio.h> in order to use that function.

H1234
08-09-2003, 06:52 PM
thats great, I love you guys!!!!


I did actually find a very crude way to get round it myself, I included <string> and assigned an empty string to a variable before doing the whole cin>>whatever.c_str() after the cout<< thing.

Seriously though, you guys are great...why didn't this stupid book tell me that???

stuka
08-11-2003, 10:36 AM
Because the book either 1) assumed you were running the program from an IDE like MS Visual Studio, which automatically pauses the application when you run it, or 2) supposed that you were running the app from a command line/terminal prompt, in which case you'd see all the output and be returned to the command prompt.

sicarius
08-11-2003, 11:33 PM
ah yes. conio.h. Thanks jamesson. I had forgotten to put that in. The devil is in the details as they say : )

3li773
08-23-2003, 05:59 PM
also you could put cin.get(); before the return 0;

then you have to press enter to close

Jamaican
09-15-2003, 10:11 PM
Originally posted by H1234
Hi, I've just started to learn C++, and I'm having a similar problem to seph up there. I typed the following code into dev version 4, and although it compiles and executes, it only does so for about half a second before it closes itself...does anyone know why?...it does this on both my desktop and laptop. Please don't spout jargon in any replys cos I'm only on, like, page 9 of the self teach book!!!!....here's the code

#include <iostream>
using namespace std; //introduces namespace std
int main()
{
cout << "Hello World" ;

return 0;
}

Thanx people

I use Dev-C++ version 4 too, I had a similar problem and swore I was going to uninstall Dev. but I think it was a Dev specialist who told me what to do, he said put: system("pause"); right before the return 0; recompile and run, he said after the program finishes it will say press any key to continue and press any key on the keyboard and the command sceen closes. I did it, and it work great, just as he said it would, and thats what I've been using ever since.

I've never tried what those guys said above me, but I know what I just told you works.

Eddie
09-16-2003, 01:16 AM
Looks like you are using a MS complier.

If you do not want to add extra code, try:


#include <iostream.h>

int main()
{
cout << "Hello World" ;

return 0;
}


It worked for me when I had that similar problem in VS .NET 2002

Jamaican
09-18-2003, 01:14 PM
Eddie he's using Dev C++ v4