C++ Beginner

Cangacosh

Solid State Member
Messages
17
I have just started programming in C++ tonight. When I run the program "Hello World" it comes up real fast and it disapears. It says it compiled right and I can see the text "Hello World" in the box before it closes. What syntax should i use to keep the box from closing.

thx in advance

Here is the code that I am using

#include <iostream.h>

void main()
{
cout <<"Hello World!\n";
}
 
If you are using vc++, make sure you are clicking run, not debug, because with run it will automayically make you press enter at the end.
 
Cangacosh said:
I have just started programming in C++ tonight. When I run the program "Hello World" it comes up real fast and it disapears. It says it compiled right and I can see the text "Hello World" in the box before it closes. What syntax should i use to keep the box from closing.

thx in advance

Here is the code that I am using

#include <iostream.h>

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

The reason it's closing like that is because it's openeing the program running it and when it's finished closes the window because the program has gone through the code and done, you haven't put a anything in there to pause it.

The best way I was taught to pause the hello world program was to use cin.get();

so it would go in the code like this:
#include <iostream.h>

void main()
{
cout <<"Hello World!\n";
cin.get();
}

That should pause the program so you can look at it. If it doesn't work you could always try getch(); but that is not really recommened because it only works in certain compilers.

you would type it like this:
#include <iostream.h>
#include <conio.h>

void main()
{
cout <<"Hello World!\n";
getch();
}
 
I had a few questions first I noticed that in the book I have it says to use "int main()" however in the code above you guys use void main() is one different from the other? Also I have a problem similiar to cang however mine is when I run a program that includes cin. I tried using cing.get(); but it still just flashes after the input is put in. Here is what the code looks like now... any help is aprecaited


#include <iostream.h>

int main()
{
cout<<"Please enter a radius so I can solve for the area"<<endl;
double radius;
cin>> radius;
cout<<"Ok, with the value "<<radius<<" you entered the area is ";
cout<<3.14*radius*radius<<endl;

cin.get();
return(0);

}
 
I didn't even notice it said void, yeah void is shouldn't be used in C++ because main is requiried to return int so use int main() however it will still work but it's not recommended as void main is used for C.

As for your program not pausing try taking out the return(0);, by the way there shouldn't be a parenthesis around the 0 it should be return 0; infact you shouldn't even bother using it, as when it gets to the end it will return a value of 0 anyway. Unless you specify a different value. I can't really test it myself as I haven't got a compiler on my computer.
 
#include <iostream>
#include <cstdlib> // used to pause :)
using namespace std; /* use this ppl!!! then you won't have to write them annoying .h's and use old libraries*/

int main ()
{
cout << "this program SHOUDLN'T flash\n";
system ("pause");
return (0);
}
 
it compiled and worked for me, i'm using Dev-C++ :) i dunno for some weird reason i can't get system ("pause"); to work on linux though

enjoy :)
 
system ("pause"); is a very bad practice to get into because it is so system intensive and is highly unrecommended so i would use cin.get(); there is never a good reason to use pause function so don't do it!

A good analogy I read was using system pause is like opening your door with a bull dozer sure it works but the key is much cleaner and more efficient.
 
Back
Top Bottom