Monday, January 31, 2011

Functions in C++

Hi guys!the program below is of a standard calculator by using switch statement.It asks for the values from the user and pass it by values.It also asks for the response from the user whether to make more computations or not.
If this program gives errors please let me know,i am ready to help any time..
May this program helps you alot!!



#include<iostream>
#include<conio.h>
using namespace std;
double calculator(float a,float b)
{
      char op;
       float c;
       cout<<"enter operator you want to perform(+,-,*,/):";
cin>>op;
      if(op=='+')
      c=a+b;
      else if(op=='-')
      c=a-b;
       else if(op=='*')
      c=a*b;
       else if(op=='/')
      c=a/b;
      return c;
      }
     
     
main()
{
do
{
      float n1,n2,x;
      char response;

cout<<"enter the first number:";
cin>>n1;
cout<<"enter second number:";
cin>>n2;

x=calculator(n1,n2);
cout<<x;
cout<<"want more(y/n)?:";
cin>>response;
}
while((response=='Y')||(response=='y'));
getch();
}

No comments:

Post a Comment