struct{
int year;
int unit;
int NumberOfSubject;
float gpa;
char semester[5];
char NameOfSubject[60][50];
char grade;
char name[40];
}student;
char CreatAccount(void)
{
int i;
char ans;
database= fopen("database.rtf", "w");
printf("\tThis is registration page\n");
printf("Please enter your name\n");
fgets(student.name,40,stdin);
printf("Is your name %s?, Y/N\n", student.name);
scanf("%c", &ans);
if (ans == 'y' || ans == 'Y')
{
printf("Registration is successfully completed!. Name on your account is %s",student.name);
fopen("database.txt", "w");
for (i=0; student.name[i] != '\n'; i++)
{
fputc(student.name[i], database);
}
fclose(database);
int i;
int j;
printf("Please enter year\n");
scanf("%d",&(student.year));
fflush(stdin);
printf("Please enter semester\n");
fgets(student.semester,10,stdin);
printf("Please enter units you have taken\n");
scanf("%d",&(student.unit));
printf("Please enter number of subjects you are taking at this moment\n");
scanf("%d",&(student.NumberOfSubject));
fflush(stdin);
for (i=0; i<student.NumberOfSubject; i++)
{
printf("Please enter the name of subjects you are taking\n");
fgets(student.NameOfSubject[i],50,stdin);
printf("Your %dth Subject is %s\n",i+1,student.NameOfSubject[i]);
}
printf("Registration is completed! Here is your information\n\n");
printf("Your year:%d\n",student.year);
printf("Semester:%s\n",student.semester);
printf("Units:%d\n",student.unit);
for (j=0; j<student.NumberOfSubject; j++)
{
printf("Name of subjects:%s",student.NameOfSubject[j]);
}
printf("Number of subjects:%d\n",student.NumberOfSubject);
}
I just can't find the cause of this problem. When the output is printed out, there are things that shouldn't be there. I want to spot what the cause is for this matter. Thanks returnFor instance, if I enter following input
2022 -year
fall-semester
12-units
3-number of subjects
math,english,physics
the output will be this,
Your year:2022
Semester:fall
english // shouldn't be printed
//shouldn't be next-line space here
Units:12
Name of subjects:english
Name of subjects:math
Name of subjects:physics
Number of subjects:3