2017-10-06 170 views
-2

我是编程新手,尝试通过自己学习,我的代码有错误,我不明白这是一个语法错误,或者我犯了错误。我使用了3个方程这是它的条件,并且是为了isntruction而做的,而do-while,if-else,with switch.After我介绍了变量a后,它向我展示了一个错误“退出非零状态”。退出非零状态

#include <stdio.h> 
#include <math.h> 

int main() { 
    float a,x,b; 
    float L; 
    int m; 

    printf("Enter variables a,x,b:"); 
    scanf("%f%f%f,&a,&x,&b"); 
    printf("For using for instruction (enter 1)"); 
    printf("For using while instruction (enter 2)"); 
    printf("For using do-while instruction (enter 3)"); 
    printf("For using ef instruction (enter 4)"); 
    scanf("%d,&m"); 
    switch(m){ 
     case 1: 
      for (x=0;x<1.2;x++) 
      { 
       L=2*cos(x-(3.14/6)); 
      } 
      for (x=0;x>= 1.2 && x<=3.9;x++) 
      { 
       L=x*x/(a+cos(powf((x+b),3))); 
      } 
      for (x=0;x>3.9;x++) 
      { 
       L=fabs(x/2*a)+powf(sin(x+1),2); 
      } 
      break; 
     case 2: 
      while (x<1.2) 
      { 
       L=2*cos(x-(3.14/6)); 
      } 
      while (x>= 1.2 && x<=3.9) 
      { 
       L=x*x/(a+cos(powf((x+b),3))); 
      } 
      while (x>3.9) 
      { 
       L=fabs(x/2*a)+powf(sin(x+1),2); 
      } 
      break; 
     case 3: 
      do 
      { 
       L=2*cos(x-(3.14/6)); 
      } 
      while (x<1.2); 
      do 
      { 
       L=x*x/(a+cos(powf((x+b),3))); 
      } 
      while (x>= 1.2 && x<=3.9); 
      do 
      { 
       L=fabs(x/2*a)+powf(sin(x+1),2); 
      } 
      while (x>3.9); 
      break; 
     case 4: 
      if (x<1.2) 
      { 
       L=2*cos(x-(3.14/6)); 
      } 
      else 
      { 
       printf("First statement is false"); 
      } 
      if(x>= 1.2 && x<=3.9) 
      { 
       L=x*x/(a+cos(powf((x+b),3))); 
      } 
      else 
      { 
       printf("Second statement is false"); 
      } 
      if(x>3.9) 
      { 
       L=fabs(x/2*a)+powf(sin(x+1),2); 
      } 
      else 
      { 
       printf("Third statement is false"); 
      } 
      break; 
     default: 
      printf("\nNo right choices\n"); 
    } 
    printf("Your answer is: L = %.3f,L"); 
} 
+1

是否执行到达终点? – Neo

+0

欢迎来到堆栈溢出!请[编辑]你的代码,以减少它到你的问题[mcve]。您当前的代码包含很多与您的问题相关的代码 - 通常,最小样本看起来与单元测试相似:只执行一项任务,输入值指定为可重现性。 –

+1

@TomKarzes不正确。见5.1.2.2.3节目终止,C标准第1段。 “...到达终止'main'函数的'}'返回值为0。 (这确实假定正在使用的编译器符合C99(或更高版本)...) –

回答

0

您缺少返回0;主要功能结束时的声明。由于c主要功能是int main()

+0

我试图把返回0;但它没有帮助 –

+2

编号Per ** 5.1.2.2.3程序终止**,[C标准]第1段(http://www.open-std。org/jtc1/sc22/wg14/www/docs/n1570.pdf):“...达到 '}' 终止 'main' 函数返回值为0。 –

+0

@AndrewHenle AFAIK一些旧的实现可能不会隐含返回0. –

3

您的问题是您的scanf参数格式不正确。

而不是scanf("%f%f%f,&a,&x,&b");使用scanf("%f%f%f",&a,&x,&b);。同样在第二个scanf

变量地址是参数,而不是字符串的一部分。

当你给它打电话时,scanf找到第一个%f,但它没有任何地址来放置该值。或者更准确地说,它会从垃圾中找到它需要的值(请参阅堆栈和参数的动态数量),因为您没有插入它。

1

scanf("%f%f%f,&a,&x,&b");应该是这样的scanf("%f%f%f",&a,&x,&b);。请更正您曾经使用过scanf的地方。由于语法错误,您的代码没有从用户那里获取输入。我已编译并尝试正确运行。请在每个地方更改scanf语法。

  1. scanf("%f%f%f,&a,&x,&b")scanf("%f%f%f",&a,&x,&b)
  2. scanf("%d,&m");scanf("%d",&m);
  3. printf("Your answer is: L = %.3f,L");printf("Your answer is: L = %.3f",L);
相关问题