我知道这对我来说可能是一个非常愚蠢的错误,但我刚刚开始学习 C。我想制作一个基本的计算器,但我不断收到关于程序的错误,该程序需要一个 const char * 但我有一个浮点数。它还说最后一个 printf 命令(我相信第 50 行)没有正确的语法或正确的形式。再次,我真的很新,很抱歉给你带来不便。谢谢大家的帮助!我的代码如下。
#include <stdio.h>
#include <math.h>
int main()
{
char firstnum, secondnum, answer;
char function;
printf("Hello and welcome to my calculator!");
printf("Please input the function you would like to use");
scanf("%c", &function);
printf("Now please input the two variables.");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
if (function == '+')
{
answer = firstnum+secondnum;
}
else if (function == '-')
{
answer = firstnum-secondnum;
}
else if (function == '*')
{
answer = firstnum*secondnum;
}
else if (function == '/')
{
answer = firstnum/secondnum;
}
else
{
printf("Sorry that was an incorrect function. The correct inputs are +, -, *, /.");
}
printf(answer);
return 0;
}