[英]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.