Thread: Student record program

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Student record program

    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:

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't open the file in id_check if you already have it open in add_record.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something wrong with output
    By preeengles in forum C Programming
    Replies: 35
    Last Post: 04-16-2009, 04:09 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. behind and confused
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-09-2001, 12:51 PM