我在c中做一个计算器,当我尝试运行它时会弹出错误

时间:2016-07-25 12:14:32

标签: c compiler-errors syntax-error

下面是代码:

#include <stdio.h>
#include <stdlib.h>

void main () {
    int op,a,b;

    printf("***Welcome to the calculatron***\n\n");

    do{
        printf("choose an operation\n\n");
        print(" 1 : Addition\n 2: Substraction\n 3: Multiplication\n 4: Division");
        scanf("%d",&op)
    }while((op != 1 && (op != 2) && (op != 3) && (op != 4));

    printf("Type the first number");
    scanf("%d",&op)
    printf("Type the second number");
    scanf("%d",&op);


    switch(op)
    case 1:
        printf("You have chosen the Addition\n");
        printf("%d + %d = %d",a,b,a+b);
        break;

    case 2:
        printf("You have chosen the Substraction\n");
        printf("%d + %d = %d",a,b,a-b);
        break;

    case 3:
        printf("You have chosen the Multiplication\n");
        printf("%d + %d = %d",a,b,a*b);
        break;

    case 4:
        printf("You have chosen the Division\n");
        printf("%d + %d = %d",a,b,a/b);
        break;
     }

错误

expected " ; " before " } " token and expected " ) " before " ; " token are in lines 13 and 41

我不知道该怎么办,请帮忙。

5 个答案:

答案 0 :(得分:1)

scanf后应该有一个分号:

 printf("Type the first number");
    scanf("%d",&op);

此外,您还缺少开关案例的左括号:

  switch(op)
{
}

答案 1 :(得分:1)

您的计划中有多处错误。

对于初学者,没有参数的函数main应该在C中声明,如

int main( void )
{
    //...
}

您忘记在scanf的语句中放置分号,例如在此代码块中

do{
    printf("choose an operation\n\n");
    print(" 1 : Addition\n 2: Substraction\n 3: Multiplication\n 4: Division");
    scanf("%d",&op)
    // must be scanf( "%d, &op );
}while((op != 1 && (op != 2) && (op != 3) && (op != 4));

while中的条件可以写得更简单,如

}while (op < 1 || op > 4 );

如果假设变量op不应接受负值,则最好将其声明为unsigned int

在这些陈述中

printf("Type the first number");
scanf("%d",&op)
printf("Type the second number");
scanf("%d",&op);

您必须输入变量ab的值,而不是变量op

printf("Type the first number");
scanf("%d",&a)
printf("Type the second number");
scanf("%d",&b);

switch语句应该有一个复合语句,你必须用括号中的语句将语句括起来

switch(op)
{
    //...
}

如果用户输入无效操作,最好包含标签default

交换机中的printf语句也包含无效的文本和操作

case 2:
    printf("You have chosen the Substraction\n");
    printf("%d + %d = %d",a,b,a-b);
    // should be printf("%d - %d = %d",a,b,a-b);
    break;

case 3:
    printf("You have chosen the Multiplication\n");
    printf("%d + %d = %d",a,b,a*b);
    // should be printf("%d * %d = %d",a,b,a * b);
    break;

case 4:
    printf("You have chosen the Division\n");
    printf("%d + %d = %d",a,b,a/b);
    // should be printf("%d / %d = %d",a,b,a / b);
    break;

最好将操作结果输出为类型long long int

例如

    printf("You have chosen the Multiplication\n");
    printf("%d * %d = %lld", a, b, ( long long int )a * b);
    break;

答案 2 :(得分:0)

{之后你还错过了switch?仔细检查编译器错误。看看你是否错过了任何语法。

答案 3 :(得分:0)

在开关(op)

之后,您缺少手镯{
switch(expression) {
    case constant-expression  :
        statement(s);
        break; /* optional */

    case constant-expression  :
        statement(s);
        break; /* optional */

   default : /* Optional */
   statement(s);
}

答案 4 :(得分:0)

您应该在发布前更多地检查您的代码。此外,你需要在编码时更加认真,你的例子有很多错误(我在这里只讨论do... while,很多人已经谈过这些错误:

  • 在第二个f
  • 上缺少printf()
  • ;
  • 之后遗失scanf()
  • while条件
  • 上缺少括号

结果是:

do {
    printf("choose an operation\n\n");
    printf("1 : Addition\n 2: Substraction\n 3: Multiplication\n 4: Division");
    scanf("%d",&op);
} while ((op != 1) && (op != 2) && (op != 3) && (op != 4));

必须对你的代码更严格,特别是在学习时,你应该注意不要养成坏习惯。