//每当我尝试添加学生信息时,它允许我输入第一个学生的详细信息,然后在按enter键后停止工作。请帮忙,我两天后有一个报告
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define NAME 50
#define LNAME 50
#define AGE 2
#define SUBJECT 200
#define ID 50
void Students();
void Subjects();
struct student{
int ID_no;
char name[20];
char lname[20];
int age[2] ;
int id_grupi;
}; struct student stu;
struct subject
{
char subject[20];
int mark[2];
};
struct subject sub;
//initializing the files used in the program
FILE *fp;
int main()
{
int choice=0;
system("cls");
printf("ntt ======================================");
printf("ntt | Options: |");
printf("ntt | |");
printf("ntt | [1] Add a student |");
printf("ntt | [2] Add subject |");
printf("ntt ======================================");
printf("n");
scanf(" %i", &choice);
switch (choice)
{
case 1:
Students();
break;
case 2:
Subjects();
default: printf("**Invalid Input.**n");
}
return (0);
}
void Students() //problem when pressing enter after giving the details
{
int select;
char choice1;
while (select!=2){
system("cls");
printf("ntt ======================================");
printf("ntt | MENU: |");
printf("ntt | |");
printf("ntt | [1] Add a student |");
printf("ntt | [2] Go back to main menu |");
printf("ntt ======================================");
printf("ntttSelect:n ");
scanf("%d", &select);
switch(select)
{
case 1:
system("cls");
printf("ntttt-ADD A STUDENT-n");
printf("NAME: ");
scanf("%s",stu.name);
printf("LAST NAME: ");
scanf("%s",stu.lname);
printf("AGE: ");
scanf("%d",stu.age);
printf("ID: ");
scanf("%d",stu.ID_no);
fp=fopen("studentfile.txt","a+");
fprintf(fp,"n%s n",stu.name);
fprintf(fp,"n%s n",stu.lname);
fprintf(fp,"n%d n",stu.age);
fprintf(fp,"n%s n",stu.ID_no);
fclose(fp);
}
}
select=1;
main();
}
void Subjects() //runs succesfully
{
char choice2;
int select2;
/* Choice2 */
while(select2!=2){
printf("ntt ======================================");
printf("ntt | MENU: |");
printf("ntt | |");
printf("ntt | [1] Add subjects per student |");
printf("ntt | [2] Main menu |");
printf("ntt ======================================");
printf("ntttSelect: ");
scanf("%d", &select2);
switch (select2)
{
case 1:
system("cls");
printf("ntttt-Add subjects-");
printf("nttID : ");
scanf("%d", &stu.ID_no);
fp=fopen("studentfile.txt","a+");
fprintf(fp,"n ID no: %dn",stu.ID_no);
int i=0,l;
printf("Add number of subjects: ");
scanf("%d",&l);
for(i=0;i<l; i++)
{
printf("Subject %d: ",i+1);
scanf("%s",sub.subject);
scanf("%s",sub.mark);
fprintf(fp,"Subject: %sn",sub.subject);
fprintf(fp,"Mark**strong text**: %sn",sub.nota);
}
fclose(fp);
}
}
select2=1;
main();**strong text** //runs the main function again
}`
使用sscanf和fgets代替scanf。像这样:
fgets(input, 50, stdin);
sscanf(input, "%d", &choice);
将输入声明为:
字符输入[50];
每次你想从stdin或文件中读取内容时,你应该使用它,它会导致一个"强程序"。
除了评论中提到的未初始化变量的所有问题外,您在Modify
函数中的fopen
存在问题。引用文档:
"w+" Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.
因此,每次使用wb+
打开Modify
中的文件时,它都会被截断为零字节。此外,您测试fopen
的方式不太好,您的错误消息也不太好。试着将其作为一般主题:
fp = fopen("studentfile.dat", "r+b");
if (fp == NULL)
perror("Unable to open studentfile.dat for read and write")
可能有其他错误.....