Wednesday, April 17, 2013

Create and search record of student details using structure

Record is collection of data items of different data types.In this program,a record is created using structure.Structure stud is created which stores student details such as roll no,name and marks.The structure stud can save 50 records.In the search functionality,specific record can be searched using roll no.

#include < stdio.h >
#include < conio.h >
struct stud
{
int roll;
char name[15];
int marks;
};
void main()
{
    struct stud a[50];
    int n,reply,sroll,option,i;   
    clrscr();
    do
    {
        printf("\n 1. Create records");
        printf("\n 2. Search record");
        printf("\n 3. exit");
        printf("\n\n Select proper option :");
        scanf("%d",&option);
        switch(option)
        {
        case 1 : // create
            printf("\nEnter n : ");
            scanf("%d",&n);
            read_data(a,n);
            break;       
        case 2 : // search
            printf("\nEnter sroll : ");
            scanf("%d",&sroll);
            reply = search_data(a,n,sroll);
            if( reply == -1)
                printf("\n Not found");
            else
            {
                printf("\nRoll\tName\tMarks");
                printf("\n%d\t%s\t%d",a[reply].roll,a[reply].name,a[reply].marks);
            }
            break;
        case 3 : exit(0);
        } //swith
    }while(1);
} // main

int read_data(struct stud a[], int n)
{
    int i;
    printf("\nEnter %d records\n",n);
    for(i=0;i < n;i++)
    {
        printf("\nRoll : "); scanf("%d",&a[i].roll);
        printf("\nName : "); flushall(); scanf("%s",a[i].name);
        printf("\nMarks : "); scanf("%d",&a[i].marks);
    } // for
    return;
} // read data

int search_data( struct stud a[], int n , int sroll)
{
    int i;
    for(i=0;i < n;i++)
    {
        if( a[i].roll == sroll)
        return(i);
    }//for
    return(-1);
}

No comments:

Post a Comment