Im a first time user of C++

Every time I write a program it ends very quickly as soon as it reaches the end of the program. After the calculations it displays the answer and then the console quickly closes. Here is my first program.

#include <iostream>
using namespace std;

int main ()
{
float ll, hh, area;
ll = 12;
hh =3;
area = ll * hh;
cout << "The Area of the rectangle is: " << area << endl;
return 0;
}

Is there anything I can do to make this better and let the console stay open longer?
add this istruction at the beginning:

 
#include <conio.h> 


and this one before return 0;

 
getch();


the getch() function waits for you press any key

Hope this can help you ;)
Last edited on
Awesome thank you so much.
Simply enter this code:
system("pause>nul");

at the end of your code betore you return any value
Hazda, NO NO NO NO NO.
Do what alex79roma said, or the educated C++ users of the world will beat you up. :)

Also, use the 'insert code' button to have your code nicely formatted:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main ()
{
float ll, hh, area;
ll = 12;
hh =3;
area = ll * hh;
cout << "The Area of the rectangle is: " << area << endl;
return 0;
}
You can also add:

cin.get();

right before

return 0;
Whats wrong wit my pausing code Azrael
It's not cross-platform. Alex's and MrGuvna's solutions would work on all computers, whereas yours is specific to Windows. It's also a lot slower compared to getch(); or cin.get();.
Topic archived. No new replies allowed.