I have been using windows all this time and recently installed Ubuntu on Virtual Box. To give Ubuntu a try,i wrote a simple calculator program.
Here's how it goes:
#include<stdio.h>
float add(float ,float ),sub(float , float ),mul(float ,float ),div(float ,float );
int main()
{
char ch;
float a,b;
printf("Enter an operator: ");
scanf("%c",&ch);
printf("Enter two values: ");
scanf("%f%f",&a,&b);
switch(ch)
{
case '+':
printf("The sum of %f and %f is %f\n",a,b,add(a,b));
break;
case '-':
printf("The substraction of %f from %f is %f\n",a,b,sub(a,b));
break;
case '*':
printf("The multiplication of %f and %f is %f\n",a,b,mul(a,b));
break;
case '/':
printf("The division of %f and %f is %f\n",a,b,div(a,b));
break;
default:
printf("\nEnter a valid operator: \n");
main();
}
return 1;
}
float add(float x,float y)
{
return (float)x + y;
}
float sub(float x,float y)
{
return (float)x-y;
}
float mul(float x,float y)
{
return (float) x*y;
}
float div(float x,float y)
{
return (float) x/y;
}
when i enter an invalid operator,it should actually read the operator and values again. But, it's asking for values directly without reading the operator. Here's a picture:
So what am i doing wrong? please explain. Thanks in advance!