制作学校管理系统并出错
Making a school management system and getting errors
我是编程和学习 C 语言的新手,正在练习结构,并试图通过使用结构来制作基本的学校管理系统,但我越来越
错误太多(通过在结构中使用指针)。那么,您能否告诉我错误是什么以及如何避免这些类型的错误。因为,根据我的理解,这应该是有效的。
这是我的代码:
#include <stdio.h>
struct student {
int rollno;
char name[20];
float marks;
};
int main() {
again:;
while (1 == 1) {
struct student array[60];
char input;
int roll;
printf("What you would like to do:\n");
printf("Enter 'R' to fill the details again of all the student\n Enter 'U' to edit the
details of a single student\n");
printf("Enter 'S' to see the details of a particular student\n Enter 'A' to see the
details of every student\n");
scanf("%c", &input);
if (input == 'A' || 'a') {
for (int i = 0; i < 60; i++) {
printf("Roll no. : %d\n Name : %s\n Marks : %f\n", array[i].rollno, array[i].name,
array[i].marks);
}
}
else if (input == 'R' || 'r') {
for (int i = 0; i < 60; i++) {
printf("Roll no. os student is : %d\n", (i + 1));
printf("Enter students name :\n");
gets(array[i].name);
printf("Enter students marks :\n");
scanf("%f", &array[i].marks);
}
}
else if (input == 'S' || 's') {
printf("Enter the roll no. of the student of whom you want to see the details :\n");
scanf("%d", &roll);
printf("Roll no. :\n %d\n", roll);
printf("Name : \n");
puts(array[(roll - 1)].name);
printf("Marks : \n %f \n", (*(array + (roll - 1))->marks));
}
else if (input == 'U' || 'u') {
printf("Enter the roll no. of the student of whom you want to see the details :\n");
scanf("%d", &roll);
printf("Enter the name :\n");
gets((*(array + (roll - 1))->name));
printf("Enter the marks :\n");
scanf("%f", &(*(array + (roll + 1))->marks));
} else {
printf("Error Occured!!! \n");
printf("Re-enter your input\n");
goto again;
}
}
return 0;
}
你的代码有很多问题:
不需要goto again;
,你已经有死循环了while (1 == 1)
,一般C写成for (;;)
,不要用goto
.
定义 struct student array[60];
应该移到循环体的范围之外。正如发布的那样,数组在每次迭代后被丢弃,其内容变得不确定。
array
应该初始化以避免在从用户读取内容之前打印内容时出现未定义的行为。
字符串文字不能跨越 C 源文件中的多行。您可以这样拆分它们以提高可读性:
printf("What you would like to do:\n"
"Enter 'R' to fill the details again of all the student\n"
"Enter 'U' to edit the details of a single student\n"
"Enter 'S' to see the details of a particular student\n"
"Enter 'A' to see the details of every student\n");
你应该有一个退出程序的菜单选项
用 scanf("%c", &input)
读取单个字符是棘手的:scanf()
将从之前的调用中读取挂起的换行符。您应该使用 scanf(" %c", &input)
,其中 space 将导致待处理的白色 space 被读取和丢弃。
if (input == 'A' || 'a')
不测试 A
或 a
,它比较 input
和 'A'
,如果不同则比较 'a'
为零,因此测试始终为真。你应该写:
if (input == 'A' || input == 'a')
gets(array[i].name);
是 NO NO。不要用gets()
,扔掉告诉你用它的书。为了与其他输入保持一致,您可以使用
scanf("%19[^\n]", array[i].name);
请注意,19
告诉 scanf()
在空终止符之前要存储到 array[i].name
中的最大字符数,以防止较长的用户输入出现未定义的行为,并且 [^\n]
导致 scanf()
读取和存储不包括换行符的字符。 %19s
会在任何白色 space 处停止,防止输入多个单词,例如 James Bond
.
scanf("%f", &(*(array + (roll + 1))->marks));
是一种很曲折的写法:
scanf("%f", &array[roll + 1].marks);
您应该检查 scanf()
的 return 值以检测无效输入并在出错时刷新挂起的输入。
这是修改后的版本:
#include <stdio.h>
struct student {
int rollno;
char name[20];
float marks;
};
int main() {
struct student array[60] = { 0 };
int len = sizeof(array) / sizeof(*array);
int c, i, roll;
char input;
for (;;) {
printf("What you would like to do:\n"
"Enter 'R' to fill the details of all the student\n"
"Enter 'U' to edit the details of a single student\n"
"Enter 'S' to see the details of a particular student\n"
"Enter 'A' to see the details of every student\n"
"Enter 'Q' to quit the program\n");
if (scanf(" %c", &input) != 1)
break;
if (input == 'A' || input == 'a') {
for (i = 0; i < len; i++) {
printf("Roll no.: %d\nName: %s\nMarks: %f\n",
array[i].rollno, array[i].name, array[i].marks);
}
} else
if (input == 'R' || input == 'r') {
for (i = 0; i < len; i++) {
printf("Roll no. os student is: %d\n", i + 1);
printf("Enter student's name:\n");
if (scanf("%19[^\n]", array[i].name) != 1)
break;
printf("Enter students marks:\n");
if (scanf("%f", &array[i].marks) != 1)
break;
}
if (i < len) {
printf("Invalid input\n");
}
} else
if (input == 'S' || input == 's') {
printf("Enter the roll no. of the student of whom you want to see the details:\n");
if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
printf("Roll no.: %d\nName: %s\nMarks: %f\n",
roll, array[roll - 1].name, array[roll - 1].marks);
} else {
printf("Invalid Roll no\n");
}
} else
if (input == 'U' || input == 'u') {
printf("Enter the roll no. of the student of whom you want to see the details:\n");
if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
printf("Enter the name:\n");
scanf("%19[^\n]", array[roll - 1].name);
printf("Enter the marks:\n");
scanf("%f", &array[roll - 1].marks);
} else {
printf("Invalid Roll no\n");
}
} else
if (input == 'Q' || input == 'q') {
return 0;
} else {
printf("Invalid entry\n");
printf("Re-enter your input\n");
}
/* read and discard the rest of the input line */
while ((c = getchar()) != EOF && c != '\n')
continue;
}
printf("Premature end of file\n");
return 0;
}
我是编程和学习 C 语言的新手,正在练习结构,并试图通过使用结构来制作基本的学校管理系统,但我越来越 错误太多(通过在结构中使用指针)。那么,您能否告诉我错误是什么以及如何避免这些类型的错误。因为,根据我的理解,这应该是有效的。
这是我的代码:
#include <stdio.h>
struct student {
int rollno;
char name[20];
float marks;
};
int main() {
again:;
while (1 == 1) {
struct student array[60];
char input;
int roll;
printf("What you would like to do:\n");
printf("Enter 'R' to fill the details again of all the student\n Enter 'U' to edit the
details of a single student\n");
printf("Enter 'S' to see the details of a particular student\n Enter 'A' to see the
details of every student\n");
scanf("%c", &input);
if (input == 'A' || 'a') {
for (int i = 0; i < 60; i++) {
printf("Roll no. : %d\n Name : %s\n Marks : %f\n", array[i].rollno, array[i].name,
array[i].marks);
}
}
else if (input == 'R' || 'r') {
for (int i = 0; i < 60; i++) {
printf("Roll no. os student is : %d\n", (i + 1));
printf("Enter students name :\n");
gets(array[i].name);
printf("Enter students marks :\n");
scanf("%f", &array[i].marks);
}
}
else if (input == 'S' || 's') {
printf("Enter the roll no. of the student of whom you want to see the details :\n");
scanf("%d", &roll);
printf("Roll no. :\n %d\n", roll);
printf("Name : \n");
puts(array[(roll - 1)].name);
printf("Marks : \n %f \n", (*(array + (roll - 1))->marks));
}
else if (input == 'U' || 'u') {
printf("Enter the roll no. of the student of whom you want to see the details :\n");
scanf("%d", &roll);
printf("Enter the name :\n");
gets((*(array + (roll - 1))->name));
printf("Enter the marks :\n");
scanf("%f", &(*(array + (roll + 1))->marks));
} else {
printf("Error Occured!!! \n");
printf("Re-enter your input\n");
goto again;
}
}
return 0;
}
你的代码有很多问题:
不需要
goto again;
,你已经有死循环了while (1 == 1)
,一般C写成for (;;)
,不要用goto
.定义
struct student array[60];
应该移到循环体的范围之外。正如发布的那样,数组在每次迭代后被丢弃,其内容变得不确定。array
应该初始化以避免在从用户读取内容之前打印内容时出现未定义的行为。字符串文字不能跨越 C 源文件中的多行。您可以这样拆分它们以提高可读性:
printf("What you would like to do:\n" "Enter 'R' to fill the details again of all the student\n" "Enter 'U' to edit the details of a single student\n" "Enter 'S' to see the details of a particular student\n" "Enter 'A' to see the details of every student\n");
你应该有一个退出程序的菜单选项
用
scanf("%c", &input)
读取单个字符是棘手的:scanf()
将从之前的调用中读取挂起的换行符。您应该使用scanf(" %c", &input)
,其中 space 将导致待处理的白色 space 被读取和丢弃。if (input == 'A' || 'a')
不测试A
或a
,它比较input
和'A'
,如果不同则比较'a'
为零,因此测试始终为真。你应该写:if (input == 'A' || input == 'a')
gets(array[i].name);
是 NO NO。不要用gets()
,扔掉告诉你用它的书。为了与其他输入保持一致,您可以使用scanf("%19[^\n]", array[i].name);
请注意,19
告诉 scanf()
在空终止符之前要存储到 array[i].name
中的最大字符数,以防止较长的用户输入出现未定义的行为,并且 [^\n]
导致 scanf()
读取和存储不包括换行符的字符。 %19s
会在任何白色 space 处停止,防止输入多个单词,例如 James Bond
.
scanf("%f", &(*(array + (roll + 1))->marks));
是一种很曲折的写法:scanf("%f", &array[roll + 1].marks);
您应该检查
scanf()
的 return 值以检测无效输入并在出错时刷新挂起的输入。
这是修改后的版本:
#include <stdio.h>
struct student {
int rollno;
char name[20];
float marks;
};
int main() {
struct student array[60] = { 0 };
int len = sizeof(array) / sizeof(*array);
int c, i, roll;
char input;
for (;;) {
printf("What you would like to do:\n"
"Enter 'R' to fill the details of all the student\n"
"Enter 'U' to edit the details of a single student\n"
"Enter 'S' to see the details of a particular student\n"
"Enter 'A' to see the details of every student\n"
"Enter 'Q' to quit the program\n");
if (scanf(" %c", &input) != 1)
break;
if (input == 'A' || input == 'a') {
for (i = 0; i < len; i++) {
printf("Roll no.: %d\nName: %s\nMarks: %f\n",
array[i].rollno, array[i].name, array[i].marks);
}
} else
if (input == 'R' || input == 'r') {
for (i = 0; i < len; i++) {
printf("Roll no. os student is: %d\n", i + 1);
printf("Enter student's name:\n");
if (scanf("%19[^\n]", array[i].name) != 1)
break;
printf("Enter students marks:\n");
if (scanf("%f", &array[i].marks) != 1)
break;
}
if (i < len) {
printf("Invalid input\n");
}
} else
if (input == 'S' || input == 's') {
printf("Enter the roll no. of the student of whom you want to see the details:\n");
if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
printf("Roll no.: %d\nName: %s\nMarks: %f\n",
roll, array[roll - 1].name, array[roll - 1].marks);
} else {
printf("Invalid Roll no\n");
}
} else
if (input == 'U' || input == 'u') {
printf("Enter the roll no. of the student of whom you want to see the details:\n");
if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
printf("Enter the name:\n");
scanf("%19[^\n]", array[roll - 1].name);
printf("Enter the marks:\n");
scanf("%f", &array[roll - 1].marks);
} else {
printf("Invalid Roll no\n");
}
} else
if (input == 'Q' || input == 'q') {
return 0;
} else {
printf("Invalid entry\n");
printf("Re-enter your input\n");
}
/* read and discard the rest of the input line */
while ((c = getchar()) != EOF && c != '\n')
continue;
}
printf("Premature end of file\n");
return 0;
}