Tuesday, September 9, 2014

search for a user (student) input data by using an array of structure, add user roll number, name, department, course, and year of joining by declaring a function name searchstudent(int)


Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create above program by declaring an array of structure., input some value from user and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
};
void searchstudent (struct student *, int );

int main()
{

 struct student s[5];
 int i,rl,p;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
 }

 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered\n\n");
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);
 }

 printf ("Enter number you want to search : ");
 scanf("%d",&rl);

 searchstudent (s ,rl);

 system("pause");
}
void searchstudent (struct student *p, int roll_no)
{
 int i,found = 0;
 for(i=0;i<5;i++)
 {
  if(p->roll_no == roll_no)
  {
   printf("record found");
   printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
   found = 1;
  }
  if (found == 0)
   printf("Not found");
 }

}

Sample Output

search user roll# name using array of structure

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact