Sunday, September 14, 2014

create an array of structure to display user input values


Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create an array of that structure. Use a loop to input values and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int roll;
char name[20];
char department[20];
char course[20];
int year;
}b[2];
int main ()
{
 int i;

 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 ("\npress any key to close.");
 getch();
}

Sample Output

using array of structure to display user input data

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