1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

code err

Discussion in 'C' started by esash, May 22, 2008.

  1. esash

    esash New Member

    Joined:
    May 21, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i wrote a simple program in C to perform a series of arithmetic operations as we do in our scientific calculators and at last get the output. the program executes but i'm not getting the expected result. plz help.
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    float answer(float a, char op, float b)
    {
    	float res;
    	switch(op)
    	{
    		case '+': res=a+b; break;
    		case '-': res=a-b; break;
    		case '*': res=a*b; break;
    		case '/': {
    				if(b!=0)
    					res=a/b;
    				else
    					printf("math error\n");
    			  } break;
    	}
    	return res;
    }
    
    void main()
    {
    	float a, b, c;
    	char op;
    	clrscr();
    	scanf("%f %c %f",&a, &op, &b);
    
    	while(b!='\n')
    	{
    		c=answer(a, op, b);
    		a=c;
    		scanf("%c %f", &op, &b);
    	}
    	printf("the result is %f\n", c);
    	getch();
    }
     
    Last edited by a moderator: May 22, 2008

Share This Page