错误请帮忙! !它说'声明终止不正确' [英] Error please help! ! It says 'declaration terminated incorrectly'

查看:77
本文介绍了错误请帮忙! !它说'声明终止不正确'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是所有代码,错误在第4行。我在Windows 10中使用带有dosbox的turbo c ++



  #include   <   stdio.h  >  
#include < span class =code-preprocessor> < conio.h >
void main()
{
clrscr();
char op;
double n1,n2;
printf( 输入运算符(+, - ,*,/):) ;
scanf( %c,& op);
printf( 输入任意两个数字:);
scanf( %lf,%lf,& n1,& n2) ;
switch operator
{
case ' +'
printf( %lf +%lf =%lf,n1,n2,n1 + n2);
break ;

case ' - '
printf( %lf - %lf =%lf, N1,N2,N1-N2);
break ;

case ' *'
printf( %lf *%lf =%lf, N1,N2,N1 * N2);
break ;

case ' /'
printf( %lf /%lf =%lf, N1,N2,N1 / N2);
break ;

默认
printf( 您输入的错误!运算符不正确);
break ;
}
getch();
}





我的尝试:



我尝试在void main()之后删除';'而不是修复它,它导致更多错误。



现在,我改变了从operator到op的变量名,并在main之后删除';'。代码运行。



但现在它返回的垃圾值而不是正确的解决方案

解决方案

( C标准修订版)您的计划有效:

  #include   <   stdio.h  >  
int main()
{
char operator ;
double n1,n2;
printf( 输入运算符(+, - ,*,/):) ;
scanf( %c,& operator);
printf( 输入任意两个数字:);
scanf( %lf,%lf,& n1,& n2) ;
switch operator
{
case ' +'
printf( %lf +%lf =%lf,n1,n2,n1 + n2);
break ;

case ' - '
printf( %lf - %lf =%lf, N1,N2,N1-N2);
break ;

case ' *'
printf( %lf *%lf =%lf, N1,N2,N1 * N2);
break ;

case ' /'
printf( %lf /%lf =%lf, N1,N2,N1 / N2);
break ;

默认
printf( 您输入的错误!运算符不正确);
break ;
}
getchar();
}





gcc 测试。我建议你使用更现代(和兼容)的 C 编译器。


引用:

我尝试在void main()之后删除';'而不是修复它,它导致更多错误

是的,这是第一个修复。

修复第一个错误并不意味着你没有其他错误。



修复它并用其他错误消息更新你的问题。



注意:operator是C ++保留的关键字,你的编译器可能是C ++编译器。



[更新]

使用调试器查看代码正在做什么。

调试器允许您逐行执行,检查变量,您将看到有一个停止的点做你期望的事。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



[更新]

你忘了运算符

  switch 运算符


Here is all the code, The error is in line 4. I am using turbo c++ with dosbox in windows 10

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	char op;
	double n1,n2;
	printf("Enter an operator(+,-,*,/):");
	scanf("%c",&op);
	printf("Enter any two numbers:");
	scanf("%lf ,%lf",&n1,&n2);
	switch(operator)
	{
	case'+':
		printf("%lf + %lf = %lf",n1,n2,n1+n2);
		break;

	case'-':
		printf("%lf - %lf = %lf",n1,n2,n1-n2);
		break;

	case'*':
		printf("%lf * %lf = %lf",n1,n2,n1*n2);
		break;

	case'/':
		printf("%lf / %lf = %lf",n1,n2,n1/n2);
		break;

	default:
		printf("Error!operator you entered is not correct");
		break;
	}
	getch();
}



What I have tried:

I tried removing the ';' after the void main() but instead of fixing it, it caused more errors.

now, i changed the variable name from operator to op and removed the ';' after main. And the code runs.

BUT now its returning garbage values instead of correct solutions

解决方案

(the 'C standard' revision of) your program works:

#include <stdio.h>
int main()
{
  char operator;
  double n1,n2;
  printf("Enter an operator(+,-,*,/):");
  scanf("%c",&operator);
  printf("Enter any two numbers:");
  scanf("%lf ,%lf",&n1,&n2);
  switch(operator)
  {
  case'+':
    printf("%lf + %lf = %lf",n1,n2,n1+n2);
    break;

  case'-':
    printf("%lf - %lf = %lf",n1,n2,n1-n2);
    break;

  case'*':
    printf("%lf * %lf = %lf",n1,n2,n1*n2);
    break;

  case'/':
    printf("%lf / %lf = %lf",n1,n2,n1/n2);
    break;

  default:
    printf("Error!operator you entered is not correct");
    break;
  }
  getchar();
}



tested with gcc. I suggeest you to use a more modern (and compliant) C compiler.


Quote:

I tried removing the ';' after the void main() but instead of fixing it, it caused more errors

Yes, it is the first fix.
Fixing the first error do not imply that you have no other errors.

Fix it and update your question with other error messages.

Beware: operator is a C++ reserved keyword and your compiler is probably a C++ compiler.

[Update]
Use the debugger to see what your code is doing.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

[Update]
You forgot an operator

switch(operator)


这篇关于错误请帮忙! !它说'声明终止不正确'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆