elseif( s == '/' && b != 0 )
return ( a / b );
return 0; //either s != +, - , * , /
// or b == 0 and s == /
//because it already checked the previous if statements
//when you use else if
#include <iostream>
usingnamespace std;
float c(float,char,float);
int main()
{
int a, b;
char s;
char endFlag = 'n';
while (endFlag != 'y')
{
cout<<"Enter what you want to calculate in the form: "<<endl;
cout<<"num+num, num-num, num*num, num/num "<<endl;
cin>>a>>s>>b;
if (b==0)
{ cout<<"Division by zero is undefined."<<endl; }
else
{cout<<"Result "<<c(a,s,b)<<endl;}
cout << "Are you done? Enter y or n: ";
cin >> endFlag; }
return 0;
}
float c(float a,char s, float b)
{
if (s=='+')
return (a+b);
elseif (s=='-')
return (a-b);
elseif (s=='*')
return (a*b);
elseif (s=='/')
return (a/b);
}
if (b==0)
{ cout<<"Division by zero is undefined."<<endl; }
you may want to change that to if( s == '/' && b == 0 ) otherwise what happens if you try and do 4 + 0? You will get an error saying you can't divide by zero. One more thing to mention you should format your main function neatly like the calculation function it makes things much easier to read.