Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
int输入后,C程序在使用Scanf时崩溃_C_Scanf - Fatal编程技术网

int输入后,C程序在使用Scanf时崩溃

int输入后,C程序在使用Scanf时崩溃,c,scanf,C,Scanf,可能是又一个愚蠢的错误,但我真的无法理解这一点。 我正在写一个基本多项式类,我的程序在输入两个整数时突然崩溃。。我试图寻找解决方案,但找不到:/ 守则: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Homework.h" #include "Fraction.h" int main() { //Input from user int degree, i;

可能是又一个愚蠢的错误,但我真的无法理解这一点。 我正在写一个基本多项式类,我的程序在输入两个整数时突然崩溃。。我试图寻找解决方案,但找不到:/

守则:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Homework.h"
#include "Fraction.h"

int main()
{
  //Input from user
  int degree, i;
  printf("Insert the degree of the polynomomial: \n");
  scanf("%d", &degree);

  //Get the coefficcients
  struct fraction *bucket = malloc((sizeof(struct fraction))*(degree + 1));
  int num;
  unsigned int den;
  for(i = 0; i < degree + 1; i++)
  {
    num = 0;
    den = 1;
    printf("Insert the coefficcient of degree %d, first num and afterwards 
the den \n", i);
    printf("Numerator:\n");
    if(scanf("%d", &num) != 1)
      printf("Input error\n");
    printf("Denominator:\n");
    if(scanf("%u", &den) != 1)
      printf("Input error\n");
    //struct fraction temp = {num, den};
    //memcpy(&bucket[0], &temp, sizeof(struct fraction));
  }

  //Check insertion
  printf("Test\n");
  //print_fraction(bucket[0]);
}
#包括
#包括
#包括
#包括“家庭作业.h”
#包括“分数.h”
int main()
{
//用户输入
int度,i;
printf(“插入多项式的次数:\n”);
scanf(“%d”、°ree);
//得到系数
结构分数*bucket=malloc((sizeof(结构分数))*(度+1));
int-num;
无符号整数;
对于(i=0;i<度+1;i++)
{
num=0;
den=1;
printf(“插入学位%d的系数,第一个数字和第二个数字
书房(i);
printf(“分子:\n”);
如果(scanf(“%d”,&num)!=1)
printf(“输入错误\n”);
printf(“分母:\n”);
如果(扫描频率(“%u”,&den)!=1)
printf(“输入错误\n”);
//结构分数temp={num,den};
//memcpy(&bucket[0],&temp,sizeof(结构分数));
}
//检查插入
printf(“测试”);
//打印_分数(桶[0]);
}
程序甚至在打印“Test”之前就退出了,要输入,我使用的是输入号码+回车键

非常感谢您的帮助

您的代码似乎运行良好。 为了编译它,我所做的唯一更改是注释掉使用malloc的那一行,并将print语句放在一行

如果您使用的是较新版本的Visual Studio,则在使用scanf函数时会出现问题。您必须使用scanf_s或禁用警告,并在顶部显示以下行:

#pragma warning(disable: 4996)

希望这能有所帮助。

发布的代码看起来不错,尽管在存储到
bucket
的注释行中存在问题。它是在提示你申请学位之前还是之后崩溃的?在输入第一个系数之前或之后,或者第二个系数之后,或者什么?@SteveSummit所以你认为这个
struct fraction*bucket=malloc((sizeof(struct fraction))*(degree+1))看起来很好?很好,但您需要检查返回值,如何根据OPs代码检查此值<代码>结构分数*bucket=malloc((sizeof(结构分数))*(度+1))=>>>
错误:“sizeof”对不完整类型“struct fraction”的应用无效。
。。。或者您有更多关于
#的信息,包括“Fraction.h”
?我们没有。添加返回值似乎可以解决问题,谢谢@SteveSummit请解释我们,如果您包含一些您没有的文件,您如何证明代码工作正常?