Thread: Why the search not working?

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    45

    Exclamation Why the search not working?

    Hello friends,
    I am newto C and was following a c tutorial to make simple student record to insert ,display, search name ,roll number and marks

    here the insert and display function works but search function shows name and roll no but showing marks = 0 eveytime?

    here is my code

    Code:
    #include <stdio.h>
    
    
    
    struct student
    {
       int rollno;
       char name[30];
       float mark;
    
    
    }stud;
    
    //    FUNCTION TO INSERT RECORDS TO THE FILE
    void insert()
    {
        FILE *fp;
        fp =fopen("Record.dat","a");
        printf("Enter the roll no: ");
        scanf("%d",&stud.rollno);
        printf("Enter the name: ");
        scanf("\t\t%s",&stud.name);
        printf("Enter the mark: ");
        scanf("\t%f",&stud.mark);
        fwrite(&stud,sizeof(stud),1,fp);
    
    
    }
    /    FUNCTION TO DISPLAY RECORDS
    void disp()
    {
        FILE *fp1;
        fp1 =fopen("Record.dat","r");
        printf("\nRoll no\t\tName\tMark\n\n");
        while(fread(&stud,sizeof(stud),1,fp1))
        {
           printf("%d\t\t%s\t%.2f\n",stud.rollno,stud.name,stud.mark);
        }
    }
    //    FUNCTION TO SEARCH THE GIVEN RECORD
    
    
    void search()
    {
        FILE *fp2;
        int r,s,avl;
        printf("\nEnter the Roll no you want to search: ");
        scanf("%d",&r);
        avl = avlrollno(r);
    
    
        if(avl== 0)
        {
            printf("Roll no %d is not available in the file\n",r);
        }
        else
        {
            fp2 = fopen("Record.dat","r");
            while(fread(&stud,sizeof(stud),1,fp2))
            {
                s = stud.rollno;
                if(s==r)
                {
                    printf("\nRoll no = %d\n",stud.rollno);
                    printf("\nName = %s\n",stud.name);
                    printf("\nMarks = %d\n",stud.mark);
                }
            }
            fclose(fp2);
        }
    }
    
    //    FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE //
    
    
    int avlrollno(int rno)
    {
        FILE *fp;
        int c = 0;
        fp =fopen("Record.dat","r");
        while(!feof(fp))
        {
            fread(&stud,sizeof(stud),1,fp);
            if(rno==stud.rollno)
            {
                fclose(fp);
                return 1;
            }
        }
        fclose(fp);
        return 0;
    }
    
    int main()
    {
    //insert();
    search();
    //disp();
    //update();
    
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    45
    thanks i got this resolved

    i was using float as marks but added integer in search function

    new code

    Code:
    void search(){
        FILE *fp2;
        int r,s,avl;
        printf("\nEnter the Roll no you want to search: ");
        scanf("%d",&r);
        avl = avlrollno(r);
    
    
        if(avl== 0)
        {
            printf("Roll no %d is not available in the file\n",r);
        }
        else
        {
            fp2 = fopen("Record.dat","r");
            while(fread(&stud,sizeof(stud),1,fp2))
            {
                s = stud.rollno;
                if(s==r)
                {
                    printf("\nRoll no = %d\n",stud.rollno);
                    printf("\nName = %s\n",stud.name);
                    printf("\nMarks = %f\n",stud.mark);
                }
            }
            fclose(fp2);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search of string array isn't working correctly
    By latte123 in forum C Programming
    Replies: 5
    Last Post: 01-04-2018, 11:43 PM
  2. Replies: 1
    Last Post: 04-16-2011, 04:44 AM
  3. Search function not working fully
    By tabstop in forum C Programming
    Replies: 7
    Last Post: 12-04-2008, 02:57 AM
  4. std::search isn't working!
    By black_spot1984 in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2008, 08:53 PM
  5. Search engines not working sometimes!
    By Bajanine in forum Tech Board
    Replies: 5
    Last Post: 12-09-2003, 09:58 PM

Tags for this Thread