【发布时间】:2015-11-04 10:26:15
【问题描述】:
我已经尝试了很多次,但我不知道出了什么问题!请帮我解决这个错误。
#include <`stdio.h>
int main(void)
{
float accum = 0, number = 0;
char oper;
printf("\nHello. This is a simple 'printing' calculator. Simply enter the number followed");
printf(" by the operator that you wish to use. ");
printf("It is possible to use the standard \noperators ( +, -, *, / ) as well as two extra ");
printf("operators:\n");
printf("1) S, which sets the accumulator; and\n");
printf("2) N, that ends the calculation (N.B. Must place a zero before N). \n");
do
{
printf("\nPlease enter a number and an operator: ");
scanf("%f %c", &number, &oper);
if (number == 0 && oper == 'N')
{
printf("Total = %f", accum);
printf("\nEnd of calculations.");
}
else if (oper == '+', '-', '*', '/', 'S')
{
switch (oper)
{
case 'S':
accum = number;
printf("= %f", accum);
break;
case '+':
accum = accum + number;
printf("= %f", accum);
break;
case '-':
accum = accum - number;
printf("= %f", accum);
break;
case '*':
accum = accum * number;
printf("= %f", accum);
break;
case '/':
if (number != 0)
{
accum = accum / number;
printf("= %f", accum);
}
else
printf("Cannot divide by zero.");
break;
default:
printf("Error. Please ensure you enter a correct number and operator.");
break;
}
}
else
printf("Error. Please ensure you enter a correct number and operator.");
}
while (oper != 'N');
return 0;
}
当我编译此代码时,我收到以下错误,如此处的快照图像所示。 snapshot of error message
【问题讨论】:
-
检查第一行#include
-
请复制粘贴编译器输出,使用图像作为文本非常烦人和脆弱(由于某些重定向失败,我无法访问图像)。
-
==运算符不是假设的any_of_these()函数。if (oper == '+', '-', '*', '/', 'S')是错误的,您可以将其重写为if (oper == '+' || oper == '-' || oper == '*'),但因为您已经有一个switch构造,您可能会放弃它以支持default块。请注意,您的错误消息 “错误。请确保您输入...” 实际上从未显示... -
您可以使用
if (strchr("+-*/S", oper))代替if (oper == '+', '-', '*', '/', 'S')。或者甚至只是删除整个if-clause,因为它与switch的default分支相同。 -
您希望
(oper == '+', '-', '*', '/', 'S')做什么?
标签: c compiler-errors