Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   code err (http://www.go4expert.com/forums/code-err-t10726/)

esash 22May2008 12:53

code err
 
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();
}



All times are GMT +5.5. The time now is 09:43.