Oct 21, 2012 at 11:13am UTC
hi, i have this program below using switch case and i want to replace if statement that appears with switch case .. can i ?
thanks for help :)
#include <iostream>
#include <cmath>
using namespace std;
float main()
{
float left, right, result;
char op;
cout<< " Please enter two numbers and the operation as follows :\n Number ( operation ) Number" << endl;
cin>>left>>op>>right;
switch (op)
{
case 'a' : case '+':
result=left+right;
break;
case 's' : case '-' :
result=left-right;
break;
case'm' : case '*' :
result=left*right;
break;
case ('d' ) : case '/' :
if ( right==0){
cout<< " you can't divide by zero!!! \n\n";
return 0;
}
else
result=left/right;
break;
case ('%') :
result=fmod(left, right );
break;
}
cout<< " The Result is : " << result<< endl;
return 0;
}
Last edited on Oct 21, 2012 at 11:33am UTC
Oct 21, 2012 at 12:11pm UTC
i tried this way but it showed me this error :
" error C2450: switch expression of type 'float' is illegal " ?
is there any way to solve it without changing " float " ?
Oct 21, 2012 at 1:06pm UTC
float main()
main should return an int, not a float.
Oct 21, 2012 at 8:23pm UTC
moschops :
it's nicely done!
thanks alot .. :)
Last edited on Oct 21, 2012 at 8:24pm UTC