c - 无法在结构中输入值

标签 c struct structure

struct student{
    char name[20]; /* student name */
    double testScore; /* test score */
    double examScore; /* exam score */
    double total; /* total score = test+exam scores */
};

void computeScore(student information);
int main()
{
struct student information[50];
                for (i = 0; i < 50; i++)
                {
                    printf("Enter the student name: ");
                    gets(information[i].name);
                    printf("\nEnter test score: ");
                    scanf("%lf",information[i].testScore);
                    printf("\nEnter exam score: ");
                    scanf("%lf",information[i].examScore);
                    computeScore(information[i]);
                }
}

我尝试输入输入测试分数,但在测试分数之后我的程序立即终止。有任何想法吗?连Enter考试分数都没有达到。

最佳答案

发生这种情况是因为变量information,它是结构“student”的一个对象,您在其中将变量定义为“double”。这意味着当程序尝试使用 to 结构读取 double 数据类型的值时发现错误。

Borland 的编译器不会链接浮点 (f-p) 库,除非我们需要它。因此,当 scanf() 或 printf() 调用中有“%f”或其他浮点 (f-p) 格式时,我们需要强制添加任何浮点 (f-p) 函数。

void dummy(double*a) {

double b=*a; //perform some floating access

dummy (&b); //calling a floating point function

}

只需在您的程序中添加此函数即可。

关于c - 无法在结构中输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446875/

相关文章:

c - 程序似乎卡在 main() 和用户定义函数之间

c# - C# 中的嵌套结构是好是坏

fortran - Netcdf 和 Fortran 结构

matlab - 在Matlab中,如何对嵌套结构进行排序?

c++ - 遍历链表和修改或插入节点C++

c++ - 将 C++ 代码从结构迁移到类

c++ - 什么可以解释调用 free() 时的堆损坏?

c - 简单 C 代码中的错误(指针)

c - libssh 绑定(bind)_接受错误 : Could not create BIO

c - 在 C 中 malloc sizeof 一个 typedef 结构