Pages

Tuesday, November 3, 2015

C Program To Create Records

The program below holds the name and earnings of an employee. A loop is set for only three employees. You can specify any number of persons in your program.

The predecessor directives come first, one to aid 'clrscr' in the program. Basic rule of c++ programming.

The records will be stored in a structure called 'struct gra'. Declarations are made within this structure. These are 'count' and 'earn' being declared as integers. So is 'name' declared as a character 'char' with the number of characters or letters it should allow [30].

The number of employees this program will hold is assigned in the loop. The next loop after void main program sets a 'start value i=0', 'end value i<3' and 'incrementer i++'. Within the first loop, the records are read in (With scanf, %d meaning the input from the user will be an integer) with a message prompting for input from the user. And within the second loop the result is displayed. Some codes like "\n" from the program means a new line. Also "getch()" helps output our result to the screen. This program is written in c language and can easily be rewritten in c++
#include <conio.h>
#include<stdio.h>
struct gra
{
int count, earn;
char name[30];
}

loop[3];
void main()
{
clrscr;

for (int i=0;i<3;i++)
{
printf("Enter the employee number: ");
scanf("%d",&loop[i].count);
printf("\nEnter Employees's name: ");
scanf("%s",&loop[i].name);
printf("\nEnter the earnings: ");
scanf("%d",&loop[i].earn);
}
for (i=0;i<3;i++)
{
printf("%4d",loop[i].count);
printf("%8s",loop[i].name);
printf("%4d",loop[i].earn);
}
getch();
}