Wednesday, 19 August 2015

Codes for a record entry project

/******************************************
  Application: Record Entry System
  Compiled on: Borland Turbo C++ 3.0
 
******************************************/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void dataentry(void);
void selectAdminOption(void);
void getData(int option);
int showAdminMenu;
void main()
{
 int cancelOption,timeOption,entryOption,exitOption;
 char choice[1];
 char selectOption[1];
 textcolor(YELLOW);
 cancelOption=0;

 /* Shows the main menu for the application*/
        while (cancelOption==0)
 {
  clrscr();
  gotoxy(30,7);
  printf("Please Select an Action-->");
  gotoxy(30,10);
  printf("Daily Time Record [1] ");
  gotoxy(30,11);
  printf("Data Entry        [2] ");
  gotoxy(30,12);
  printf("Close             [3] ");
  gotoxy(30,15);
  printf("Please Enter Your Choice (1/2/3): ");
  scanf("%s",&choice);
  timeOption=strcmp(choice,"1");
  entryOption=strcmp(choice,"2");
  exitOption=strcmp(choice,"3");

  if (timeOption==0)
  {
  clrscr();
  gotoxy(23,6);
  printf("DAILY EMPLOYEE TIME RECORDING SYSTEM");
  gotoxy(16,24);
  printf("Input Any Other key to Return to Previous Screen.");
  gotoxy(31,9);
  printf("[1] Employee Log In ");
  gotoxy(31,10);
  printf("[2] Employee Log Out");
  gotoxy(28,12);
  printf("Please Enter Your Option: ");
  scanf("%s",&selectOption);
  if (strcmp(selectOption,"1")==0)
  {
     getData(5);
  }
  if (strcmp(selectOption,"2")==0)
  {
     getData(6);
  }
  cancelOption=0;
  }
  if (entryOption==0)
  {
  dataentry();
  cancelOption=0;
  }
  if (exitOption==0)
  {
  cancelOption=1;
  }
  if (!(timeOption==0 || entryOption==0 || exitOption==0))
  {
                   gotoxy(10,17);
     printf("You Have Entered an Invalid Option. Please Choose Either 1, 2 or 3. ");
     getch();
     cancelOption=0;
  }
 }
 clrscr();
 gotoxy(23,13);
 printf("The Application will Close Now. Thanks!");
 getch();
}
/* This function provides logic for data entry to be done for the system.
Access to Data Entry screens will be only allowed to administrator user.*/
void dataentry(void)
{
char adminName[10], passwd[5],buffer[1];
char tempo[6],sel[1];
int validUserNameOption,validUserPwdOption,returnOption,UserName,inc,tmp;
char plus;
 clrscr();
 validUserNameOption=0;
 validUserPwdOption=0;
 while (validUserPwdOption==0)
 {
  clrscr();
  while (validUserNameOption==0)
  {
   clrscr();
   gotoxy(20,5);
   printf("IT SOFTWARE DATA ENTRY SYSTEM-ADMIN INTERFACE");
   gotoxy(20,24);
   printf("Info: Type return to go back to the main screen.");
   gotoxy(28,10);
   printf("Enter Administrator Name: ");
   scanf("%s",&adminName);
   returnOption=strcmp(adminName,"return");
   UserName=strcmp(adminName,"admin");
   if (returnOption==0)
   {
   goto stream;
   }
   if (!(UserName==0 || returnOption==0))
   {
   gotoxy(32,11);
   printf("Administrator Name is Invalid.");
   getch();
   validUserNameOption=0;
   }
   else
   validUserNameOption=1;
  }
 gotoxy(30,11);
 printf("Enter Password: ");
 inc=0;
 while (inc<5)
 {
  passwd[inc]=getch();
  inc=inc+1;
  printf("* ");
 }
 inc=0;
 while (inc<5)
 {
  tempo[inc]=passwd[inc];
  inc=inc+1;
 }
 while(getch()!=13);
 if (!strcmp(tempo, "admin12"))
  {
                        gotoxy(28,13);
   printf("You have Entered a Wrong Password. Please Try Again. ");
   getch();
   validUserPwdOption=0;
   validUserNameOption=0;
  }
  else
  {
   clrscr();
   gotoxy(24,11);
   textcolor(YELLOW+BLINK);
   cprintf("You Have Successfully Logged In.");
   gotoxy(24,17);
   textcolor(YELLOW);
   printf("Press Any Key to Continue.");
   validUserPwdOption=1;
   validUserNameOption=1;
   getch();
   showAdminMenu=0;
       while (showAdminMenu==0)
       {
   clrscr();
   gotoxy(24,4);
   printf("ADMIN OPTIONS");
   gotoxy(26,9);
   printf("Add New Employee       [1]");
   gotoxy(26,11);
   printf("Show Daily Entries     [2]");
   gotoxy(26,13);
   printf("Search Employee Record [3]");
   gotoxy(26,15);
   printf("Remove Employee        [4]");
   gotoxy(26,17);
   printf("Close                  [5]");
   gotoxy(24,21);
   printf("Please enter your choice: ");
   selectAdminOption();
     }
  }
 }
stream:{}
}
/* This function provides the administrator level functionalities, such as Adding or deleting an employee.*/
void selectAdminOption(void)
{
  char chc[1];
  int chooseNew,chooseShow,chooseSearch,chooseRemove,chooseClose;
  gets(chc);
  chooseNew=strcmp(chc,"1");
  chooseShow=strcmp(chc,"2");
  chooseSearch=strcmp(chc,"3");
  chooseRemove=strcmp(chc,"4");
  chooseClose=strcmp(chc,"5");
  if (!(chooseNew==0 || chooseShow==0 || chooseSearch==0 || chooseRemove==0 || chooseClose==0))
  {
     gotoxy(19,21);
     textcolor(RED+BLINK);
     cprintf("Invalid Input!");
     gotoxy(34,21);
     textcolor(YELLOW);
     cprintf("Press any key to continue.");
  }
  if (chooseNew==0)
  {
     clrscr();
     gotoxy(25,5);
     getData(1);
  }
  else if(chooseShow==0)
  {
     getData(2);
  }
  else if(chooseSearch==0)
  {
     clrscr();
     getData(3);
  }
  else if(chooseRemove==0)
  {
     getData(4);
  }
  else if (chooseClose==0)
  {
     showAdminMenu=1;
  }
}
/* This function retreives data from the database as well as do data processing according to user requests.
   The function provides functionality for menu options provided to both employee as well as administrator user*/
void getData(int option)
{
 FILE *db,*tempdb;
 char anotherEmp;
 int choice;
 int showMenu,posx,posy;
 char checkSave,checkAddNew;
 int i;
 struct employee
 {
        char firstname[30];
 char lastname[30];
 char password[30];
 int  empid;
 char loginhour;
 char loginmin;
 char loginsec;
 char logouthour;
 char logoutmin;
 char logoutsec;
 int yr;
 char mon;
 char day;
 };
 struct employee empData;
 char confirmPassword[30];
 long int size;
 char lastNameTemp[30],firstNameTemp[30],password[30];
 int searchId;
 char pass[30];
 char findEmployee;
 char confirmDelete;
 struct date today;
 struct time now;
 clrscr();
 /* Opens the Employee Database*/
 db=fopen("d:/empbase.dat","rb+");
 if(db==NULL)
 {
  db=fopen("d:/empbase.DAT","wb+");
  if(db==NULL)
  {
   printf("The File could not be opened.\n");
   exit();
  }
 }
  printf("Application Database \n");
  size=sizeof(empData);
  showMenu=0;
  while(showMenu==0)
  {
   fflush(stdin);
   choice=option;
   /* Based on the choice selected by admin/employee, this switch statement processes the request*/
   switch(choice)
   {
   /* To add a new employee to the database*/
   case 1:
    fseek(db,0,SEEK_END);
    anotherEmp='y';
    while(anotherEmp=='y')
    {
                   checkAddNew=0;
     while(checkAddNew==0)
     {
     clrscr();
     gotoxy(25,3);
     printf("ADD A NEW EMPLOYEE");
     gotoxy(13,22);
     printf("Warning: Password Must Contain Six(6) AlphaNumeric Digits.");
     gotoxy(5,8);
     printf("Enter First Name: ");
     scanf("%s",&firstNameTemp);
     gotoxy(5,10);
     printf("Enter Last Name: ");
     scanf("%s",&lastNameTemp);
     gotoxy(43,8);
     printf("Enter Password: ");
     for (i=0;i<6;i++)
     {
      password[i]=getch();
      printf("* ");
     }
     password[6]='\0';
     while(getch()!=13);
     gotoxy(43,10);
     printf("Confirm Password: ");
     for (i=0;i<6;i++)
     {
      confirmPassword[i]=getch();
      printf("* ");
     }
     confirmPassword[6]='\0';
     while(getch()!=13);
     if (strcmp(password,confirmPassword))
     {
        gotoxy(24,12);
               printf("Passwords do not match.");
        gotoxy(23,13);
        printf("Press any key to continue.");
        getch();
     }
     else
     {
     checkAddNew=1;
     rewind(db);
     empData.empid=0;
     while(fread(&empData,size,1,db)==1);
     if (empData.empid<2000)
     empData.empid=20400;
     empData.empid=empData.empid+1;
     gotoxy(29,16);
     printf("Save Employee Information? (y/n): ");
     checkSave=getche();
     if (checkSave=='y')
     {
     strcpy(empData.firstname,firstNameTemp);
     strcpy(empData.lastname,lastNameTemp);
     strcpy(empData.password,password);
     empData.loginhour='t';
     empData.logouthour='t';
     empData.day='j';
     fwrite(&empData,size,1,db);
     }
     gotoxy(28,16);
     printf("                        ");
     gotoxy(28,16);
     printf("Would like to add another employee? (y/n):");
     fflush(stdin);
     anotherEmp=getche();
     printf("\n");
     }
     }
    }
    break;
   /* To view time records for all employees*/
   case 2:
    clrscr();
    gotoxy(21,2);
    printf("VIEW EMPLOYEE INFORMATION");
    gotoxy(1,5);
    printf("Employee ID  Employee Name     Time Logged In     Time Logged Out     Date\n\n");
    rewind(db);
    posx=3;
    posy=7;
    while(fread(&empData,size,1,db)==1)
    {
     empData.firstname[0]=toupper(empData.firstname[0]);
     empData.lastname[0]=toupper(empData.lastname[0]);
     gotoxy(posx,posy);
     printf("%d",empData.empid);
     gotoxy(posx+10,posy);
     printf("| %s, %s",empData.lastname,empData.firstname);
     gotoxy(posx+30,posy);
     if (empData.loginhour=='t')
     {
      printf("| Not Logged In");
     }
     else
     printf("| %d:%d:%d",empData.loginhour,empData.loginmin,empData.loginsec);
     gotoxy(posx+49,posy);
     if (empData.logouthour=='t')
     {
     printf("| Not Logged Out");
     }
     else
     printf("| %d:%d:%d",empData.logouthour,empData.logoutmin,empData.logoutsec);
     if (empData.day=='j')
     {
     gotoxy(posx+69,posy);
     printf("| No Date");
     }
     else
     {
     gotoxy(posx+73,posy);
     printf("| %d/%d/%d",empData.mon,empData.day,empData.yr);
     }
     posy=posy+1;
    }
           getch();
    printf("\n");
    break;
  /* To search a particular employee and view their time records*/
  case 3:
    clrscr();
    gotoxy(27,5);
    printf("SEARCH EMPLOYEE INFORMATION");
    gotoxy(25,9);
    printf("Enter Employee Id to Search: ");
    scanf("%d", &searchId);
    findEmployee='f';
    rewind(db);
           while(fread(&empData,size,1,db)==1)
    {
        if (empData.empid==searchId)
        {
  gotoxy(33,11);
  textcolor(YELLOW+BLINK);
  cprintf("Employee Information is Available.");
  textcolor(YELLOW);
  gotoxy(25,13);
  printf("Employee name is: %s %s",empData.lastname,empData.firstname);
  if(empData.loginhour=='t')
  {
  gotoxy(25,14);
  printf("Log In Time: Not Logged In");
  }
  else
  {
  gotoxy(25,14);
  printf("Log In Time is: %d:%d:%d",empData.loginhour,empData.loginmin,empData.loginsec);
  }
  if(empData.logouthour=='t')
  {
  gotoxy(25,15);
  printf("Log Out Time: Not Logged Out");
  }
  else
  {
  gotoxy(25,15);
  printf("Log Out Time is: %d:%d:%d",empData.logouthour,empData.logoutmin,empData.logoutsec);
  }
  findEmployee='t';
        getch();
        }
    }
    if (findEmployee!='t')
    {
    gotoxy(30,11);
    textcolor(YELLOW+BLINK);
    cprintf("Employee Information not available. Please modify the search.");
    textcolor(YELLOW);
    getch();
    }
    break;
  /* To remove entry of an employee from the database*/
  case 4:
    clrscr();
    gotoxy(25,5);
    printf("REMOVE AN EMPLOYEE");
    gotoxy(25,9);
    printf("Enter Employee Id to Delete: ");
    scanf("%d", &searchId);
    findEmployee='f';
    rewind(db);
           while(fread(&empData,size,1,db)==1)
    {
        if (empData.empid==searchId)
        {
  gotoxy(33,11);
  textcolor(YELLOW+BLINK);
  cprintf("Employee Information is Available.");
  textcolor(YELLOW);
  gotoxy(25,13);
  printf("Employee name is: %s %s",empData.lastname,empData.firstname);
  findEmployee='t';
        }
    }
    if (findEmployee!='t')
    {
    gotoxy(30,11);
    textcolor(YELLOW+BLINK);
    cprintf("Employee Information not available. Please modify the search.");
    textcolor(YELLOW);
    getch();
    }
    if (findEmployee=='t')
    {
    gotoxy(29,15);
    printf("Do you want to Delete the Employee? (y/n)");
    confirmDelete=getche();
  if (confirmDelete=='y' || confirmDelete=='Y')
  {
  tempdb=fopen("d:/tempo.dat","wb+");
  rewind(db);
  while(fread(&empData,size,1,db)==1)
   {
    if (empData.empid!=searchId)
    {
    fseek(tempdb,0,SEEK_END);
    fwrite(&empData,size,1,tempdb);
    }
   }
  fclose(tempdb);
  fclose(db);
  remove("d:/empbase.dat");
  rename("d:/tempo.dat","d:/empbase.dat");
  db=fopen("d:/empbase.dat","rb+");
  }
    }
    break;
  /* To login an employee into the system and record the login date and time*/
  case 5:
    clrscr();
    gotoxy(20,4);
    printf("DAILY EMPLOYEE TIME RECORDING SYSTEM");
    gotoxy(20,23);
    printf("Warning: Please Enter Numeric Values Only.");
    gotoxy(23,7);
    printf("Enter Your Id to Login: ");
    scanf("%d", &searchId);
    gotoxy(20,23);
    printf("                                            ");
    findEmployee='f';
    rewind(db);
           while(fread(&empData,size,1,db)==1)
    {
        if (empData.empid==searchId)
        {
  gotoxy(23,8);
  printf("Enter Your Password: ");
                 for (i=0;i<6;i++)
     {
      pass[i]=getch();
      printf("* ");
     }
     pass[6]='\0';
   while(getch()!=13);
  if (strcmp(empData.password,pass))
  {
   gotoxy(23,11);
   textcolor(YELLOW+BLINK);
   cprintf("You Have Supplied a Wrong Password.");
   textcolor(YELLOW);
   findEmployee='t';
   getch();
   break;
  }
  gotoxy(23,11);
  textcolor(YELLOW+BLINK);
  cprintf("You have successfully Logged In the System.");
  textcolor(YELLOW);
  gotoxy(23,13);
  printf("Employee name: %s %s",empData.lastname,empData.firstname);
  gettime(&now);
  getdate(&today);
  gotoxy(23,14);
  printf("Your LogIn Time: %2d:%2d:%2d",now.ti_min,now.ti_hour,now.ti_sec);
  gotoxy(23,15);
  printf("Your Log In Date: %d/%d/%d",today.da_mon,today.da_day,today.da_year);
  empData.day=today.da_day;
  empData.mon=today.da_mon;
  empData.yr=today.da_year;
  fseek(db,-size,SEEK_CUR);
  empData.loginhour=now.ti_min;
  empData.loginmin=now.ti_hour;
  empData.loginsec=now.ti_sec;
  fwrite(&empData,size,1,db);
  findEmployee='t';
  getch();
        }
    }
    if (findEmployee!='t')
    {
    gotoxy(30,11);
    textcolor(YELLOW+BLINK);
    cprintf("Employee Information is not available.");
    textcolor(YELLOW);
    getch();
    }
    break;
  /* To logout an employee and record the logout date and time*/
  case 6:
           clrscr();
    gotoxy(20,4);
    printf("DAILY EMPLOYEE TIME RECORDING SYSTEM");
    gotoxy(20,23);
    printf("Warning: Please Enter Numeric Values Only.");
    gotoxy(23,7);
    printf("Enter Your Id to Logout: ");
    scanf("%d", &searchId);
    gotoxy(20,23);
    printf("                                            ");
    findEmployee='f';
    rewind(db);
           while(fread(&empData,size,1,db)==1)
    {
        if (empData.empid==searchId)
        {
  gotoxy(23,8);
  printf("Enter Password: ");
                 for (i=0;i<6;i++)
     {
      pass[i]=getch();
      printf("* ");
     }
     pass[6]='\0';
   while(getch()!=13);
  if (strcmp(empData.password,pass))
  {
   gotoxy(30,11);
   textcolor(YELLOW+BLINK);
   cprintf("You Have Supplied a Wrong Password.");
   textcolor(YELLOW);
   findEmployee='t';
   getch();
   break;
  }
  gotoxy(23,11);
  textcolor(YELLOW+BLINK);
  cprintf("You have successfully Logged Out of the System.");
  textcolor(YELLOW);
  gotoxy(23,13);
  printf("Employee name is: %s %s",empData.lastname,empData.firstname);
  gettime(&now);
  getdate(&today);
  gotoxy(23,14);
  printf("Your Log Out Time: %2d:%2d:%2d",now.ti_min,now.ti_hour,now.ti_sec);
  gotoxy(23,15);
  printf("Your Log Out Date: %d/%d/%d",today.da_mon,today.da_day,today.da_year);
  fseek(db,-size,SEEK_CUR);
  empData.logouthour=now.ti_min;
  empData.logoutmin=now.ti_hour;
  empData.logoutsec=now.ti_sec;
  fwrite(&empData,size,1,db);
  findEmployee='t';
  getch();
        }
    }
    if (findEmployee!='t')
    {
    gotoxy(23,11);
    textcolor(YELLOW+BLINK);
    cprintf("Employee Information is not available.");
    textcolor(YELLOW);
    getch();
    }
    break;
  /* Show previous menu*/
  case 9:
    printf("\n");
    exit();
    }
   fclose(db);
   showMenu=1;
   }
 }

Why we learn C language today?

  •  There are some other language like as C++,C# and Java, its languages are more advanced then c,then why we learn C today? There are some reason behind this question that's are following:
    1. C is the basic(mother) of all other languages.When you had learned c, you are easily deal with classes,object,template,structure,polymorphic and with familiar terminology.
    2. C++,C# and Java are OOP(Object Oriented Programming) languages.So when u creating object u must still a good command on C language.
    3. Major parts of OS(Operating System) like as Windows,UNIX,Linux are still written in C.
    4. C language is known for owns speed and execution.
    5. Today daily routine apparatus like as microwave,washing,digital camera's,microprocessor,and operating system,smart phones are worked in C programs.
    6. Maximum 3-d games are built in C.
  • So now let's start to learn C.

    C Basic

    #. What is language? 
    • A language is medium in which two or more object/person communicate their thought,information,data etc. C is computer language. Computer languages are generally two types:
      1. High Level Language-Translator required must example: FORTRAN
      2. Low Level Language-Not requirement translator example: machine language(0 , 1)
    •  C is a high level language with low level programming features so it is also called procedure object oriented language i.e. we can write program either in machine language(0,1) or general English keyword(HLL).
    •  C is a structured programming language,that is developed by Dennis Ritchie in 1972 at AT & Bell Laboratories in USA. 
    • Some basic tips for C Programs and C language:
      1. Each instruction in a C program is written as a separate statement. Therefore, a complete C program would comprise a series of statements.
      2. The statement in a program must appear in the same order in which we wish them to be executed, unless the result will be differ as we want wish.
      3. All statement are entered in Small case letters.
      4. Every C statement must be end with a semicolon (;). This is act that statement has been terminated
      5. C is "free form language" i.e. there are no specific rule for where the statement write.

    Programming in C to test that the given number is prime or not

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n,x=2;
    clrscr();
    printf("\n Enter the number for testing (Prime or not) :");
    scanf("%d",&n);
    while (x<n)
    {
    if (n%x==0)
    {
    printf("\n The number %d is not Prime.",n);
    getch();
    exit(0);
    }
    x++;
    }
    printf("\n The number %d is Prime.",n);
    getch();
    }

    Programming in C to find the cube of any number

    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
    int y,x;
    clrscr();
    printf("\nEnter a number for its cube");
    scanf("%d",&x);
    y=pow(x,3);
    printf("\nCube of the given number is %d ",y);
    getch();
    }

    Programming in C to find the square of the given number

    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
    int y,x;
    clrscr();
    printf("\nEnter a number for their square");
    scanf("%d",&x);
    y=pow(x,2);
    printf("\nSquare of the given number is %d ",y);
    getch();
    }

    Programming in C to reverse a number

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,r=0,s;
    clrscr();
    printf("\nEnter the number which is to be reversed (upto 4 numbers only):");
    scanf("%d",&a);
    while(a>0)
    {
    s=a%10;
    a=a-s;
    r=(r*10)+s;
    a=a/10;
    }
    printf("\n Reversed number: %d",r);
    getch();
    }