Thursday, July 30, 2009

I dont know why i can get my result shown in this operation. this is a simple calculator. pls anyone help?

#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


float a;


float b;


float c;





float result;


int op;


int option;


int main()





{


do





{


printf("\n\n\nEnter First Number =");


scanf("%f",%26amp;a);


printf("\n\n\n\Enter Second Number \n");


scanf("%f",%26amp;b);





printf("Please enter an Option..\n");


printf("[1] Add \n");


printf("[2] Substract \n");


printf("[3] Multiply \n");


printf("[4] Devide \n ");


scanf("%f",%26amp;c);





switch (op)


{


case 1 : // Case Addition


result=a+b;


break;


case 2 : //Case Substraction


result=a-b;


break;


case 3 : // Case multiplication


result=a*b;


break;


case 4 : // Case Division


result=a/b;


}


printf("\n\nThe Result is=%f",a,b,c);


printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n... an Option..\n");


printf("[0] Exit.\n");


printf("[1] Continue. \n");








} while (option==1);





getch();


return 0;


}

I dont know why i can get my result shown in this operation. this is a simple calculator. pls anyone help?
You're using the variable op in the switch statement. Where does op get its value?





You're using the variable option in the while loop. Where does option get its value?





How did you use the variable c in


scanf("%f",%26amp;c)?





The c here should be used int the switch statement as well as in the while condition.





Your code can be rewritten as:


------------------------------


#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


float a;


float b;





float result;





int op;





int main(){





do{





printf("\n\n\nEnter First Number =");


scanf("%f",%26amp;a);


printf("\n\n\n\Enter Second Number \n");


scanf("%f",%26amp;b);





printf("Please enter an Option..\n");


printf("[1] Add \n");


printf("[2] Substract \n");


printf("[3] Multiply \n");


printf("[4] Devide \n ");


printf("[5] Exit\n");





scanf("%d",%26amp;op);





switch (op)


{


case 1 : // Case Addition


result=a+b;


break;


case 2 : //Case Substraction


result=a-b;


break;


case 3 : // Case multiplication


result=a*b;


break;


case 4 : // Case Division


result=a/b;


}





printf("\n\nThe Result is=%f",a,b,c);





} while (option!=5);





getch();


return 0;


}





---------------------





Hope this helps.

song downloads

No comments:

Post a Comment