Sunday, September 14, 2014

write a program that allows the user to input data and search


Problem

Create a structure to specify data on students given below:
Roll number, Name, Department, Course, Year of joining
Create an array of structure. Use a loop to input values 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>
void searchstudent (int roll_no);
struct book
{
 int roll;
 char name[20];
 char department[20];
 char course[20];
 int year;
}b[2];
int main ()
{
 int i, roll_no;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("SEARCH DATA(enter roll number): ");
 scanf ("%d",&roll_no);

 searchstudent(roll_no);

 printf ("\npress any key to close.");
 getch();
}
void searchstudent (int roll_no)
{
 int i;

 for (i=0;i<2;i++)
 {
  if (roll_no == b[i].roll)
   printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
 }
}

Sample Output

search of a student using 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