Hello guys, i posted this already yesterday, and i kinda worked it out already..however..
Code:
void add_record(record x, FILE *fp)
{
char ans;
int tempid, count;
do
{
system("cls");
INPUT:
printf("Student ID: "); scanf("%d", &tempid);
count=id_check(tempid,x);
if(count==1)
{
printf("\nStudent number already exists. Try again\n");
getch();
system("cls");
goto INPUT;
}
else
x.idno=tempid;
fflush(stdin);
printf("First Name: "); gets(x.Fname);
printf("Last Name; "); gets(x.Lname);
strupr(x.Lname);
printf("Course: "); gets(x.course);
printf("Age: "); scanf("%d", &x.age);
fflush(stdin);
printf("Tuition: "); scanf("%f", &x.tuition);
system("cls");
printf("Enter another record? Y/N");
ans=toupper(getch());
system("cls");
fwrite(&x,sizeof(x),1,fp);
}while(ans=='Y');
fclose(fp);
}
int id_check(int tempid, record x)
{
int count=0,a=0;
FILE *fp;
record temp[100];
fp=fopen("students_record.txt","r");
while(!feof(fp))
{
fread(&temp[a],sizeof(record),1,fp);
a++;
}
a-=1;
for(int i=0;i<a;i++)
{
if(tempid==temp[i].idno)
count++;
}
if(count>0)
return 1;
else
return 0;
}
If i input a number that already exists, i got it to work already that it asks for another number, however though, if i input a new number, then after inputting all the info, i input another record using the same number, it doesn't display anymore that the number already exists.
example:
Enter number: 1
First Name:
.
.
.
.
Input another record? Y/N
Y
Enter number: 1 <----i used the same number but the program continues
First Name: