Hi, I am new to programming and have been working on a calculator for some time. I am trying to add some trigger functions and I am having problems with the sine. Other functions work (+, -, *, /), but when I insert the "sine", it skips part of the code where it says that this is an incorrect function. Please help with my code. Thanks!
#include <stdio.h> #include <math.h> int main() { float firstnum, secondnum, angle, answer, pi; char function, sine; pi = atan(1.0)*4; printf("\nHello and welcome to my calculator!\n"); while(1) { printf("\nPlease input the function you would like to use. These include +, -, *, /, sine.\n"); scanf("%s", &function); switch(function) { case '+': printf("\nNow please input the two variables.\n"); scanf("%f", &firstnum); scanf("%f", &secondnum); answer = firstnum+secondnum; break; case '-': printf("\nNow please input the two variables.\n"); scanf("%f", &firstnum); scanf("%f", &secondnum); answer = firstnum-secondnum; break; case '*': printf("\nNow please input the two variables.\n"); scanf("%f", &firstnum); scanf("%f", &secondnum); answer = firstnum*secondnum; break; case '/': printf("\nNow please input the two variables.\n"); scanf("%f", &firstnum); scanf("%f", &secondnum); answer = firstnum/secondnum; break; case 'sine': printf("\nPlease enter the angle.\n"); scanf("%f", &angle); answer = sin(angle); break; default: printf("Sorry, that is an incorrect function. The only available choices are +, -, *, /, sine."); break; } printf("Your answer is %f \n", answer); printf("\nWhen you are ready to quit, simply press Ctrl + C or just hit the X button in the top right.\n"); } return 0; }
source share