#include<iostream>
#include<conio.h>
#include<cmath>
usingnamespace std;
int main()
{//start of program
int prog=0;
do{
cout<<"\t\t\t**WORK AND POWER CALCULATOR**\n"<<endl;
cout<<"[1] Work Calculator\n"<<endl;
cout<<"[2] Power Calculator\n"<<endl;
cout<<"[3] Exit\n"<<endl;
cout<<"Select Program from 1-3: ";
cin>>prog;
cout<<endl;
{
if(prog>=1 && prog<=3)
{
switch(prog)
{//switch starts
{
case 1:
float F=0, d=0, theta=0, W=0, angle=0;
cout<<"\t\t\t **WORK CALCULATOR**\n"<<endl;
getch();
cout<<"Use positive values...\n"<<endl;
getch();
do
{
cout<<"Enter value for F (force in N): ";
cin>>F;
cout<<endl;
}while(F<=-1);
do
{
cout<<"Enter value for d (displacement in m): ";
cin>>d;
cout<<endl;
}while(d<=-1);
do
{
cout<<"Enter value for theta (angle in deg.)\nnote: use 0 if there is no angle: ";
cin>>theta;
cout<<endl;
}while(theta<=-1);
if(theta>0)
{
angle=theta*(3.1416/180);
double W=F*d*cos(angle);
cout<<"Work is : "<<W<<" J (joule)\n"<<endl;
getch();
break;
}else W=F*d;
cout<<"Work is : "<<W<<" J (joule)\n"<<endl;
getch();
break;
}//case 1 ends
{
case 2:
float F=0, v=0, Pw=0, Php=0;
cout<<"\t\t\t **POWER CALCULATOR**\n"<<endl;
getch();
cout<<"Use positive values...\n"<<endl;
getch();
do
{
cout<<"Enter value for F (force in N): ";
cin>>F;
cout<<endl;
}while(F<=-1);
do
{
cout<<"Enter value for v (velocity in m/s): ";
cin>>v;
cout<<endl;
}while(v<=-1);
Pw=F*v;
Php=Pw/745.699872;
cout<<"Power is : "<<Pw<<" watts or "<<Php<<" hp\n"<<endl;
getch();
break;
}//case 2 ends
{
case 3: cout<<"\t\t **THANK YOU FOR USING THIS CALCULATOR!**\n"<<endl;
getch();
return 0;
break;
}//case 3 ends
}//switch ends
}else cout<<"\t\t Invalid selection!!! Please try again... \n"<<endl;
}
}while(prog!=3);
getch();
return 0;
}//end of program
int prog=0;
do
{
cin>>prog;
if (prog>=1 && prog<=3) {}
else cout<<"Please try again... \n"<<endl;
} while (prog!=3);
Bad input does set the failbit for cin on line 4 and prog certainly will not be one of 1, 2, or 3.
On the following iterations the failbit is already set, so operator>> on line 4 will automatically fail.
You have to check for errors and clear them before processing more input.