简体   繁体   English

输入“正弦”时,C计算器程序始终显示错误?

[英]C calculator program keeps giving error when I input “sine”?

Hi I am new to programming and have been working on a calculator for a while now. 嗨,我是编程新手,现在已经从事计算器工作了一段时间。 I am trying to add some trig functions in and I am having trouble with sine. 我试图在其中添加一些三角函数,但正弦函数遇到了麻烦。 The other functions work (+, -, *, /) but when I put in "sine" it skips to the part of the code where it says it is an incorrect function. 其他功能也可以工作(+,-,*,/),但是当我输入“ sine”时,它会跳到代码部分,指出它是不正确的功能。 Please help out 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;
}
'sine'

That is a multi-character literal. 那是一个多字符文字。 function is a single character. function是单个字符。 It's integral value is checked in the switch statement. 它的整数值在switch语句中检查。 You will likely never be able to consume a single character from the user which matches sine in the way that you are attempting to do so. 您可能永远无法以试图使用的方式消耗用户匹配sine的单个字符。 Read a string (a char* ) instead. 改为读取一个字符串( char* )。

From the standard: 从标准:

C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (eg, 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined." C99 6.4.4.4p10:“包含多个字符(例如,'ab')或包含未映射到单字节执行字符的字符或转义序列的整数字符常量的值是实现定义的”。

C does not have a first class string type. C没有一流的字符串类型。 This means that you cannot use a switch statement for strings, you will need to use functions such as strlcmp for string comparison. 这意味着您不能对字符串使用switch语句,需要使用诸如strlcmp之类的函数进行字符串比较。

Depending on your objective (either making a calculator, or learning C) it might be wise to either switch to a different language with higher abstraction levels, or start with lower level exercises from a good C textbook. 根据您的目标(制作计算器或学习C语言),明智的方法是切换到具有较高抽象级别的另一种语言,或者从一本好的C教科书中进行较低级别的练习开始。

Also, please be aware that working with strings and user input correctly in C, that is without security holes, is much more difficult than it would seem at first. 另外,请注意,在C中正确使用字符串和用户输入(即没有安全漏洞)比起初看起来要困难得多。 If your objective is learning a language perhaps learning C++ is a better bet where you have std::string to handle your comparisons and iostreams to handle your input and output. 如果您的目标是学习语言,那么在拥有std::string来处理比较结果和iostreams来处理输入和输出结果的地方,最好学习C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM