简体   繁体   English

当我尝试计算学生笔记平均值时程序停止工作

[英]Program stop working when I try to calculate student note average

Looks like node* findAverage function can't recieve pointer or head value.看起来node* findAverage函数无法接收指针或head值。 I'm not very good at structs so I couldn't find a proper solution.我不太擅长结构,所以我找不到合适的解决方案。 I think there's something wrong about p=head .我认为p=head I can run program perfectly with only 1 student but can't increase it.我可以只用 1 个学生完美地运行程序,但不能增加它。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
  int number;
  char name[20];
  char surname[20];
  float MidTermGrade;
  float FinalGrade;
  float yearEndGrade;
  struct student *next;
};

typedef struct student node;
node *head, *newNode;

node *createList() {
  int n = 3, k;
  node *head, *p;

  for (k = 0; k < n; k++) {
    if (k == 0) {
      head = (node *)malloc(sizeof(node));
      p = head;
    }

    else {
      p->next = (node *)malloc(sizeof(node));
      p = p->next;
    }

    printf("Enter %d. student number: ", k + 1);
    scanf("%d", &(p + k)->number);
    printf("Enter %d. student name: ", k + 1);
    scanf("%s", &(p + k)->name);
    printf("Enter %d. student surname: ", k + 1);
    scanf("%s", &(p + k)->surname);
    printf("Enter %d. student Mid Term Grade: ", k + 1);
    scanf("%f", &(p + k)->MidTermGrade);
    printf("Enter %d. student Final Grade: ", k + 1);
    scanf("%f", &(p + k)->FinalGrade);
  }
}

node *findAverage() {
  int n = 3, k;
  node *p;
  p = head;

  for (k = 0; k < n; k++) {
    printf("control point");
    (p + k)->yearEndGrade =
        ((p + k)->MidTermGrade * 0.4) + ((p + k)->FinalGrade * 0.6);

    printf("Year-End Grade is: %.2f\n", (p + k)->yearEndGrade);
  }

  p->next = NULL;
  return head;
}

int main() {
  node *head;

  createList();

  findAverage();
}

No output, just "student.exe stopped working."没有输出,只是“student.exe 停止工作”。 error.错误。

edit: I tried debugging dev c++ and it stopped working too lol编辑:我尝试调试 dev c++,但它停止工作了,哈哈

By the way, you're coding in C language, so your language tag seems to be wrong.顺便说一句,你是用 C 语言编码的,所以你的语言标签似乎是错误的。 createList() should not be declared a separate local variable head ( use the global variable instead). createList()不应声明为单独的局部变量head (改用全局变量)。 To avoid memory leakage all allocations must be freed at the end ( see destroy() function)为避免内存泄漏,必须在最后释放所有分配(请参阅 destroy() 函数)

struct student {
    int number;
    char name[20];
    char surname[20];
    float MidTermGrade;
    float FinalGrade;
    float yearEndGrade;
    struct student *next;
};

typedef struct student node;
node *head, *newNode;

void createList(int n) {
    node *p; 
    for (int k = 0; k < n; k++) {
        if (k == 0) {
            head = (node*)malloc(sizeof(node));
            p = head;
        }
        else {
            p->next = (node*)malloc(sizeof(node));
            p = p->next;
        }
        printf("Enter %d. student number: ", k + 1);
        scanf("%d", &(p + k)->number);
        printf("Enter %d. student name: ", k + 1);
        scanf("%s", &(p + k)->name);
        printf("Enter %d. student surname: ", k + 1);
        scanf("%s", &(p + k)->surname);
        printf("Enter %d. student Mid Term Grade: ", k + 1);
        scanf("%f", &(p + k)->MidTermGrade);
        printf("Enter %d. student Final Grade: ", k + 1);
        scanf("%f", &(p + k)->FinalGrade);
    }
}

node *findAverage(int n) {
    node *p;
    p = head;
    for (int k = 0; k < n; k++) {
        printf("control point");
        (p + k)->yearEndGrade =
            ((p + k)->MidTermGrade * 0.4) + ((p + k)->FinalGrade * 0.6);

        printf("Year-End Grade is: %.2f\n", (p + k)->yearEndGrade);
    }
    p->next = NULL;
    return head;
}

void destroy() {
    while (head) {
        node *p = head;
        head = head->next;
        free(p);
    }
}

int main() {
    node *head;
    createList(3);
    findAverage(3);
    destroy();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 它是c中的学生日记程序,当我尝试通过获取编译器来输入学生的姓名时,它会跳过名称,它可与scanf一起使用,但无法输入空格 - Its a student diary program in c.when i try to input name of student by gets compiler skips name it works with scanf but it cant input spaces C程序计算平均值 - C program calculate average 在 c 中,我尝试从文件中获取一个学生信息并尝试将该信息放入 struct 但它不起作用 - In c I try to get one student information from a file and try to put that informations into struct but it is not working C程序计算一个学生的成绩 - C program to calculate a student's grade 我想编写一个程序来使用结构计算学生的结果并打印位置 - I want to write a program to calculate the result of a student by using the structure and print the position 我想创建一个程序来使用结构计算学生的结果并打印位置 - I want to create a program to calculate the result of a student by using the structure and print the position 可用的malloc内存时程序停止工作 - program stop working when free malloc memory 我的 c 打印学生详细信息的程序无法正常工作 - my c program to print student details is not working C程序停止工作(m [0] - &gt; rows = i;) - C Program stop working (m[0] -> rows = i;) 当我尝试编译这个程序时,我在程序错误中得到了&#39;#&#39; - I get stray '#' in program error when I try to compile this program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM