【问题标题】:Program crashes when I compile, run and enter the integer value for variable "c" [closed]当我编译,运行并输入变量“c”的整数值时程序崩溃[关闭]
【发布时间】:2021-10-13 17:02:58
【问题描述】:

当我在 Dev C++ 中运行这个程序时,它会一直执行,直到提示输入变量“c”的整数值。然后,我输入值,整个程序崩溃。没有语法错误;问题是当我不使用 If(Condition) 语句时它运行良好。

[下图显示了我编译和运行程序时的输出。][1]

// Program to display patterns of numbers according to the input values
#include <stdio.h>
int main() {
  int i, j, in, ni, jn, nj, c;
  printf("For the statements for(i=ni;i<=/>=in;i++/--) and\n");
  printf("For(j=nj;j<=/>=jn;j++/--),\n");
  printf("\nEnter ni: ");
  scanf("%d", &ni);
  printf("\nEnter in: ");
  scanf("%d", &in);
  printf("\nEnter nj: ");
  scanf("%d", &nj);
  printf("\nEnter jn: ");
  scanf("%d", &jn);
  printf("\nSelect your choice for the type of for loop to be used:\n");
  printf("\n01. for(i=ni;i<=in;i++) & for(j=nj;j<=jn;j++)\n");
  printf("\n02. for(i=ni;i<=in;i++) & for(j=nj;j<=jn;j--)\n");
  printf("\n03. for(i=ni;i<=in;i++) & for(j=nj;j>=jn;j--)\n");
  printf("\n04. for(i=ni;i<=in;i++) & for(j=nj;j>=jn;j++)\n");
  printf("\n05. for(i=ni;i<=in;i--) & for(j=nj;j<=jn;j++)\n");
  printf("\n06. for(i=ni;i<=in;i--) & for(j=nj;j<=jn;j--)\n");
  printf("\n07. for(i=ni;i<=in;i--) & for(j=nj;j>=jn;j--)\n");
  printf("\n08. for(i=ni;i<=in;i--) & for(j=nj;j>=jn;j++)\n");
  printf("\n09. for(i=ni;i>=in;i++) & for(j=nj;j<=jn;j++)\n");
  printf("\n10. for(i=ni;i>=in;i++) & for(j=nj;j<=jn;j--)\n");
  printf("\n11. for(i=ni;i>=in;i++) & for(j=nj;j>=jn;j--)\n");
  printf("\n12. for(i=ni;i>=in;i++) & for(j=nj;j>=jn;j++)\n");
  printf("\n13. for(i=ni;i>=in;i--) & for(j=nj;j<=jn;j++)\n");
  printf("\n14. for(i=ni;i>=in;i--) & for(j=nj;j<=jn;j--)\n");
  printf("\n15. for(i=ni;i>=in;i--) & for(j=nj;j>=jn;j--)\n");
  printf("\n16. for(i=ni;i>=in;i--) & for(j=nj;j>=jn;j++)\n");
  printf(
      "\nInput must be between 1 and 16 inclusive(Including 1 and 16 too)\n");
  scanf("%d", &c);
  if (c == 1) {
    for (i = ni; i <= in; i++) {
      for (j = nj; j <= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 2) {
    for (i = ni; i <= in; i++) {
      for (j = nj; j <= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 3) {
    for (i = ni; i <= in; i++) {
      for (j = nj; j >= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 4) {
    for (i = ni; i <= in; i++) {
      for (j = nj; j >= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 5) {
    for (i = ni; i <= in; i--) {
      for (j = nj; j <= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 6) {
    for (i = ni; i <= in; i--) {
      for (j = nj; j <= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 7) {
    for (i = ni; i <= in; i--) {
      for (j = nj; j >= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 8) {
    for (i = ni; i <= in; i--) {
      for (j = nj; j >= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c == 9) {
    for (i = ni; i >= in; i++) {
      for (j = nj; j <= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 10) {
    for (i = ni; i >= in; i++) {
      for (j = nj; j <= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 11) {
    for (i = ni; i >= in; i++) {
      for (j = nj; j >= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 12) {
    for (i = ni; i >= in; i++) {
      for (j = nj; j >= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 13) {
    for (i = ni; i >= in; i--) {
      for (j = nj; j <= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 14) {
    for (i = ni; i >= in; i--) {
      for (j = nj; j <= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 15) {
    for (i = ni; i >= in; i--) {
      for (j = nj; j >= jn; j--) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c = 16) {
    for (i = ni; i >= in; i--) {
      for (j = nj; j >= jn; j++) {
        printf("%d", i);
      }
      printf("\n");
    }
  } else if (c < 1 || c > 16) {
    printf("Invalid choice input. Please enter numbers between 1 and 16 only.");
  }
  return (0);
}

【问题讨论】:

  • 你需要将一个指向 c ( &c) 的指针传递给 scanf
  • 这看起来无效:printf("For(j=nj;j&lt;=/&gt;=jn;j++/--),\n");
  • @BillLynch 我想我该睡觉了 :)
  • if(c=10) 真的
  • 欢迎您。请查看How-to-askMCVE。与演示问题所需的代码相比,您的代码非常长。您可以(并且应该)轻松删除大部分 ifprintf 指令。如果您希望人们提供帮助,请不要让他们费力地阅读大量没有任何价值的代码。

标签: c for-loop if-statement dev-c++


【解决方案1】:

这可能与编译器应该发出的所有警告有关。

<source>:34:16: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%d",c);
           ~~  ^
<source>:134:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=10)
                ~^~~
<source>:134:18: note: place parentheses around the assignment to silence this warning
        else if(c=10)
                 ^
                (   )
<source>:134:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=10)
                 ^
                 ==
<source>:145:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=11)
                ~^~~
<source>:145:18: note: place parentheses around the assignment to silence this warning
        else if(c=11)
                 ^
                (   )
<source>:145:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=11)
                 ^
                 ==
<source>:156:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=12)
                ~^~~
<source>:156:18: note: place parentheses around the assignment to silence this warning
        else if(c=12)
                 ^
                (   )
<source>:156:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=12)
                 ^
                 ==
<source>:167:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=13)
                ~^~~
<source>:167:18: note: place parentheses around the assignment to silence this warning
        else if(c=13)
                 ^
                (   )
<source>:167:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=13)
                 ^
                 ==
<source>:178:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=14)
                ~^~~
<source>:178:18: note: place parentheses around the assignment to silence this warning
        else if(c=14)
                 ^
                (   )
<source>:178:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=14)
                 ^
                 ==
<source>:189:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=15)
                ~^~~
<source>:189:18: note: place parentheses around the assignment to silence this warning
        else if(c=15)
                 ^
                (   )
<source>:189:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=15)
                 ^
                 ==
<source>:200:18: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        else if(c=16)
                ~^~~
<source>:200:18: note: place parentheses around the assignment to silence this warning
        else if(c=16)
                 ^
                (   )
<source>:200:18: note: use '==' to turn this assignment into an equality comparison
        else if(c=16)
                 ^
                 ==
<source>:34:16: warning: variable 'c' is uninitialized when used here [-Wuninitialized]
    scanf("%d",c);
               ^
<source>:5:26: note: initialize the variable 'c' to silence this warning
    int i,j,in,ni,jn,nj,c;
                         ^
                          = 0
9 warnings generated.

【讨论】:

  • 只有第一个警告与分段错误有关。
猜你喜欢
  • 1970-01-01
  • 2019-05-30
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2012-08-07
  • 2014-11-18
相关资源
最近更新 更多