Im a first time user of C++

Jan 23, 2008 at 3:10pm
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?
Jan 23, 2008 at 3:25pm
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 Jan 23, 2008 at 3:27pm
Jan 23, 2008 at 3:30pm
Awesome thank you so much.
Jan 23, 2008 at 6:01pm
Simply enter this code:
system("pause>nul");

at the end of your code betore you return any value
Jan 23, 2008 at 6:23pm
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;
}
Jan 23, 2008 at 10:51pm
You can also add:

cin.get();

right before

return 0;
Jan 24, 2008 at 6:45pm
Whats wrong wit my pausing code Azrael
Jan 25, 2008 at 1:09pm
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.