NO

Author Topic: Programm gets closed within second of dsplaying result  (Read 2410 times)

mnrindia

  • Guest
Programm gets closed within second of dsplaying result
« on: July 05, 2011, 06:31:52 PM »
Hai I have written this code.Its working fine when i execute in pelles.But when i open .exe file After i enter radius it is closing with in a second.Please help.
MY CODE IS


#include <stdio.h>
#define PI 3.1419
int main()
{
float area , radius ;
printf("Enter the radius of the circle in cms.");
scanf("%f",&radius);
area = PI * radius * radius;
printf("The area of a circle is %f sq.cms.",area);
printf("A Progrramm devoloped by Nishanth. ");
printf("Thank you for using ");
printf("Press Esc to exit");
return 0;
}

CommonTater

  • Guest
Re: Programm gets closed within second of dsplaying result
« Reply #1 on: July 05, 2011, 08:40:31 PM »
When you run a console program by clicking on it the console window closes as soon as the program exits.

This leaves you 2 choices...
1) Open a CMD prompt in the program's folder and run it by typing in it's name and hitting enter.

2) Add an extra line of code...
Code: [Select]
 
printf("Press any key to exit");
getchar();                                   <-- add this.
return 0;

The getchar() call will hold the console window open until you press a key on your keyboard...


You may also have to add a getchar() after your call to scanf() since it leaves the Enter key in the keyboard buffer.
« Last Edit: July 05, 2011, 08:53:01 PM by CommonTater »