Subject: How can we stop a programme at any point?
Assalam o Alaikum, Sir how can we stop a c++ programme at any point? for example if area of inner circle is putted greater or equal to area of outer circle in a programme for finding area of ring. How can i switch out without further process?
Posts: 95
Threads: 19
Joined: Oct 2010
Dear Student,
As you have already learned the use of conditional statements in previous lectures. We can use those conditional statements to perform the required functionality to check for the correct or incorrect input. For example, we can use if else statement to check if radius of outer circle is greater than inner circle or not and proceed accordingly. Below is the modifed program code which contains the required logic.
#include <iostream.h>
using namespace std;
double circleArea ( double);
int main ( )
{
double rad1 ;
double rad2 ;
double ringArea ;
cout << "Please enter the outer radius value:\t" ;
cin >> rad1 ;
cout << "Please enter the radius of the inner circle:\t" ;
cin >> rad2 ;
if(rad1 < rad2)
{
cout<<"\nOuter radius should not greater than inner radius";
cout<<endl<<endl;
}
else
{
ringArea = circleArea(rad1) - circleArea(rad2);
cout<< " Area of the ring having inner raduis " << rad2 << " and the outer radius " <<rad1 << " is " << ringArea ;
cout<<endl<<endl;
}
system("pause");
}
double circleArea ( double radius )
{
return ( 3.1415926 * radius * radius ) ;
}
You can also use other condition statements for implementing the above logic. Try using switch statement to see if you can do it or not.