Does the calculator program continue to give an error when I enter the sine?

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; } 
+4
source share
2 answers
 'sine' 

This is a multi-character literal. function is one character. Its integral value is checked in the switch . You probably can never use a single character from a user that matches sine in the way that you are trying to do. Read the line (a char* ).

From the standard:

C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (for example," ab ") or containing a character or escape sequence that does not map to a single-byte execution character from the implementation."

+6
source

C does not have a first class string type. This means that you cannot use the switch statement for strings, you will need to use functions such as strlcmp to compare strings.

Depending on your goal (creating a calculator or learning C) it may be wise to either switch to another language with a higher level of abstraction, or start with lower-level exercises from a good C textbook.

Also, keep in mind that working with strings and user input correctly in C, which does not contain security holes, is much more complicated than it would seem at first glance. If your goal is to learn the language, perhaps learning C ++ is the best bet in which you have std::string to handle your comparisons and iostreams to handle input and output.

0
source

All Articles