这是一个c的bug吗?有兴趣的看一看!

时间:2021-11-21 14:12:04
为什么下面的程序无法在turbo c2.0上运行?
================================================================
#include <stdio.h>

struct stu{
  float subscore;
  char subname[20];
}students[6];

int main() {
  int i=0;
  
  for (i=0;i<6;i++) {
    printf("\nPLS Input The Student's name:");
    scanf("%s",students[i].subname);
    printf("\nPLS Input The Student's score:");
    scanf("%f",&students[i].subscore);
    printf("\n%f",students[i].subscore);
  }
  return 0;
}
====================================
错在哪里?
但这样就可以运行:
===========================================================
#include <stdio.h>

struct stu{
  float subscore;
  char subname[20];
}students;

int main() {
  int i=0;
  
    printf("\nPLS Input The Student's name:");
    scanf("%s",students.subname);
    printf("\nPLS Input The Student's score:");
    scanf("%f",&students.subscore);
    printf("\n%f",students.subscore);
  return 0;
}

24 个解决方案

#1


这个是TC2.0的bug, 不是C的bug.

#2


谁的bug都不是,你的数据类型申明错误!

#3


这样定义结构体数组是可以的.

#4


语法没错,对吧?

#5


#include <stdio.h>

typedef struct stu{
  float subscore;
  char subname[20];
}STUDENT;

STUDENT students[6];

#6


这个问题已经有很多人提出来了。用 scanf 输入浮点数时,不能使用数组。
即:
float f[2];
for(int n=0; n<2; n++)
    scanf("%f", &f[n]); 有问题。
解决方案,用一个临时变量:
float f[2], temp;
for(int n=0; n<2; n++) {
    scanf("%f", &temp);
    f[n] = temp;
}

#7


to jishiping(JSP 季世平) 是这样的吗?以前没有注意过了。

#8


我也很郁闷,执行你的代码出现问题,但是使用Borland C++ 3.0自带的例程,就没有问题,现在贴下来供大家讨论。当我把例程精简到和你的问题一样的程度,就也发生error了。
#include <stdio.h>
#include <conio.h>

int main(void)
{
   char label[20];
   char name[20];
   int entries = 0;
   int loop, age;
   double salary;

   struct Entry_struct
   {
      char  name[20];
      int   age;
      float salary;
   } entry[20];

/* Input a label as a string of characters restricting to 20 characters */
   printf("\n\nPlease enter a label for the chart: ");
   scanf("%20s", label);
   fflush(stdin);  /* flush the input stream in case of bad input */

/* Input number of entries as an integer */
   printf("How many entries will there be? (less than 20) ");
   scanf("%d", &entries);
   fflush(stdin);   /* flush the input stream in case of bad input */

/* input a name restricting input to only letters upper or lower case */
   for (loop=0;loop<entries;++loop)
   {
      printf("Entry %d\n", loop);
      printf("  Name   : ");
      scanf("%[A-Za-z]", entry[loop].name);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input an age as an integer */
      printf("  Age    : ");
      scanf("%d", &entry[loop].age);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input a salary as a float */
      printf("  Salary : ");
      scanf("%f", &entry[loop].salary);
      fflush(stdin); /* flush the input stream in case of bad input */
   }

/* Input a name, age and salary as a string, integer, and double */
   printf("\nPlease enter your name, age and salary\n");
   scanf("%20s %d %lf", name, &age, &salary);


/* Print out the data that was input */
   printf("\n\nTable %s\n",label);
   printf("Compiled by %s  age %d  $%15.2lf\n", name, age, salary);
   printf("-----------------------------------------------------\n");
   for (loop=0;loop<entries;++loop)
      printf("%4d | %-20s | %5d | %15.2lf\n",
         loop + 1,
         entry[loop].name,
         entry[loop].age,
         entry[loop].salary);
   printf("-----------------------------------------------------\n");
   return 0;
}

#9


说明,我试过在scanf("%f",students[i].subscore)前加fflush(stdin),也不好使。

#10


to jishiping(JSP 季世平) 
真的吗?

#11


关注

#12


我还没注意到呢.

#13


我也遇到这样的问题。
是TC的BUG。
我老师也这么说。

#14


说实话,我很反对楼上的朋友的理由。"我老师也这么说。",老师算个屁!

#15


好像原因处在这里:
struct {
  float subscore;
  char subname[20];
}students[6];
这样就可以了,试试??

#16


在tc++3.0上可以轻松搞定的啊

#17


呵呵,我从来就用的tc3.0

#18


用括号改变结合顺序就行了
TC2.0的结合律有问题

scanf("%f",&(students[i].subscore));

#19


嗯,小CASE

#20


用VC7或者BC6吧。我N年前用的东西,太土了。

#21


括号是好东西,不能确定的话就用它.

#22


to farad(watt):
不行啊!
===============
to acking(阿肯哥):
也不行啊!
===================
to jishiping(JSP 季世平):
很不幸,你说的方法也不行呀!
为什么?!
===========================
很遗憾,大家似乎都没有说对。有谁真正调试成功过,请告诉我.

#23


肯定是TC的BUG,scanf对结构中的浮点数输入上有问题。

我看了天子.圣堂的贴,试了一下,发现了一个补救措施:

只要你另外定义一个float变量,然后再fscan它在程序最后,就没错了。

程序可改为:

#include <stdio.h>

struct stu{
  float subscore;
  char subname[20];
}students[6];

int main() {
  int i=0;
  float end;
  
  for (i=0;i<6;i++) {
    printf("\nPLS Input The Student's name:");
    scanf("%s",students[i].subname);
    printf("\nPLS Input The Student's score:");
    scanf("%f",&students[i].subscore);
    printf("\n%f",students[i].subscore);
  }

    //这样看起来自然一些
    printf("\nPress any key to continue.:");
    scanf("%f",end);

  return 0;
}

我可是调试通过了的,给我和天子加分吧

#24


to steelking88(旋) :
=====================
你的方法是没错,可是为什么是这样?

#1


这个是TC2.0的bug, 不是C的bug.

#2


谁的bug都不是,你的数据类型申明错误!

#3


这样定义结构体数组是可以的.

#4


语法没错,对吧?

#5


#include <stdio.h>

typedef struct stu{
  float subscore;
  char subname[20];
}STUDENT;

STUDENT students[6];

#6


这个问题已经有很多人提出来了。用 scanf 输入浮点数时,不能使用数组。
即:
float f[2];
for(int n=0; n<2; n++)
    scanf("%f", &f[n]); 有问题。
解决方案,用一个临时变量:
float f[2], temp;
for(int n=0; n<2; n++) {
    scanf("%f", &temp);
    f[n] = temp;
}

#7


to jishiping(JSP 季世平) 是这样的吗?以前没有注意过了。

#8


我也很郁闷,执行你的代码出现问题,但是使用Borland C++ 3.0自带的例程,就没有问题,现在贴下来供大家讨论。当我把例程精简到和你的问题一样的程度,就也发生error了。
#include <stdio.h>
#include <conio.h>

int main(void)
{
   char label[20];
   char name[20];
   int entries = 0;
   int loop, age;
   double salary;

   struct Entry_struct
   {
      char  name[20];
      int   age;
      float salary;
   } entry[20];

/* Input a label as a string of characters restricting to 20 characters */
   printf("\n\nPlease enter a label for the chart: ");
   scanf("%20s", label);
   fflush(stdin);  /* flush the input stream in case of bad input */

/* Input number of entries as an integer */
   printf("How many entries will there be? (less than 20) ");
   scanf("%d", &entries);
   fflush(stdin);   /* flush the input stream in case of bad input */

/* input a name restricting input to only letters upper or lower case */
   for (loop=0;loop<entries;++loop)
   {
      printf("Entry %d\n", loop);
      printf("  Name   : ");
      scanf("%[A-Za-z]", entry[loop].name);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input an age as an integer */
      printf("  Age    : ");
      scanf("%d", &entry[loop].age);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input a salary as a float */
      printf("  Salary : ");
      scanf("%f", &entry[loop].salary);
      fflush(stdin); /* flush the input stream in case of bad input */
   }

/* Input a name, age and salary as a string, integer, and double */
   printf("\nPlease enter your name, age and salary\n");
   scanf("%20s %d %lf", name, &age, &salary);


/* Print out the data that was input */
   printf("\n\nTable %s\n",label);
   printf("Compiled by %s  age %d  $%15.2lf\n", name, age, salary);
   printf("-----------------------------------------------------\n");
   for (loop=0;loop<entries;++loop)
      printf("%4d | %-20s | %5d | %15.2lf\n",
         loop + 1,
         entry[loop].name,
         entry[loop].age,
         entry[loop].salary);
   printf("-----------------------------------------------------\n");
   return 0;
}

#9


说明,我试过在scanf("%f",students[i].subscore)前加fflush(stdin),也不好使。

#10


to jishiping(JSP 季世平) 
真的吗?

#11


关注

#12


我还没注意到呢.

#13


我也遇到这样的问题。
是TC的BUG。
我老师也这么说。

#14


说实话,我很反对楼上的朋友的理由。"我老师也这么说。",老师算个屁!

#15


好像原因处在这里:
struct {
  float subscore;
  char subname[20];
}students[6];
这样就可以了,试试??

#16


在tc++3.0上可以轻松搞定的啊

#17


呵呵,我从来就用的tc3.0

#18


用括号改变结合顺序就行了
TC2.0的结合律有问题

scanf("%f",&(students[i].subscore));

#19


嗯,小CASE

#20


用VC7或者BC6吧。我N年前用的东西,太土了。

#21


括号是好东西,不能确定的话就用它.

#22


to farad(watt):
不行啊!
===============
to acking(阿肯哥):
也不行啊!
===================
to jishiping(JSP 季世平):
很不幸,你说的方法也不行呀!
为什么?!
===========================
很遗憾,大家似乎都没有说对。有谁真正调试成功过,请告诉我.

#23


肯定是TC的BUG,scanf对结构中的浮点数输入上有问题。

我看了天子.圣堂的贴,试了一下,发现了一个补救措施:

只要你另外定义一个float变量,然后再fscan它在程序最后,就没错了。

程序可改为:

#include <stdio.h>

struct stu{
  float subscore;
  char subname[20];
}students[6];

int main() {
  int i=0;
  float end;
  
  for (i=0;i<6;i++) {
    printf("\nPLS Input The Student's name:");
    scanf("%s",students[i].subname);
    printf("\nPLS Input The Student's score:");
    scanf("%f",&students[i].subscore);
    printf("\n%f",students[i].subscore);
  }

    //这样看起来自然一些
    printf("\nPress any key to continue.:");
    scanf("%f",end);

  return 0;
}

我可是调试通过了的,给我和天子加分吧

#24


to steelking88(旋) :
=====================
你的方法是没错,可是为什么是这样?