Share Pastebin
Guest
Public paste!

part 3 code

By: a guest | Mar 21st, 2010 | Syntax: C | Size: 12.69 KB | Hits: 58 | Expires: Never
Copy text to clipboard
  1.                /*Computer Science Internal Assesment
  2.                   David Gardner Grade 13-c
  3.                   Ms. A. Jones
  4.                   Karate Registration System
  5.                   Last Modification Date: 01/03/10 01:39*/
  6.  
  7. #include <stdio.h>//Standard library files for standard input and output functions
  8. #include <conio.h>/*Common Input and Output functions & Functions for
  9.                             manipulating strings#include <stdio.h>*/
  10. #include <stdlib.h>//other miscellanous standard library functtions
  11. #include <string.h>//standard libraray functions for strings
  12. #define MAX 25//define array length for structures
  13. #define size 90000000//define max number of characters something can store
  14. int i;//global variable used as a counter variable
  15. int numstud;
  16. struct student//structure to hold student non numerical data
  17. {
  18.      char fname[25],midname[25],lname[25],gender[10],bltrank[10];
  19. }stu[MAX];
  20.  
  21. struct numdata//structure to hold student numerical data
  22. {
  23.        int age,year,id,gen,quu;//gen variable used to hold numerical input for gender[see regmode()
  24. }num[MAX];
  25.  
  26. void header();//function to create a header in the screen display
  27. void login();//function to request password from user
  28. void menu();//funtion to list the main menu of the program
  29. void regmode();//function used to input data of registering students
  30. void viewmode();//funtion used to view the stored data of the organization
  31. void setup();//function used to set the password and for the I.D. funtion to word
  32. int idnum(int x);//creates an I.D. number for each entry
  33.  
  34. main(void)//start of the program
  35. {
  36.    header();
  37.    printf("\n\tWelcome to the Jamaica Karate Club Registration System.");
  38.    login();
  39.    system("cls");//function in stdlib.h to clear the screen
  40.    menu();
  41.    getch();      
  42. }
  43.  
  44. void header()
  45. {// "\t" used for tab
  46.     printf("\n\t\t\t||~~~~~~~~~~~~~~~~~~~~~~~~~||\n");
  47.     printf("\t\t\t||~~FUTURE LEADERS EAGLES~~||\n");
  48.     printf("\t\t\t||~~~~KARATE CLUB & GYM~~~~||\n");
  49.     printf("\t\t\t||~~~~~~~~~~~~~~~~~~~~~~~~~||\n");
  50. }
  51.  
  52. void login()
  53. {
  54.      char pwrd[20],xwrd[20];
  55.      FILE *passcheck;//using a pointer to reference to a file
  56.      passcheck = fopen("syspass","r");//opening the file in READ mode
  57.      if(passcheck==NULL)//Checking if the file is empty
  58.      {
  59.          system("cls");
  60.          menu();
  61.      }
  62.      else//if file contains data do.....
  63.      {
  64.          printf("\nEnter Password: ");
  65.          scanf("%s",&pwrd);
  66.          fscanf(passcheck,"%s",xwrd);//scans data from file into a variable
  67.          int z=3;
  68.          while(strcmp(pwrd,xwrd)!=0)/*strcmp used to cpmare the input data with
  69.                                       the data from the file...while they are not
  70.                                       the same (!=0) do.....*/
  71.          {
  72.               printf("\nIncorrect Password...try again\n");
  73.               scanf("%s",&pwrd);
  74.               z-=1;
  75.               if(z == 0)//password login attempts are used up
  76.               {
  77.                   printf("\nAccess Denied\n");
  78.                   system("pause");
  79.                   exit(0);
  80.               }
  81.          }
  82.          system("cls");
  83.          menu();
  84.      }
  85.      fclose(passcheck);//closing the file
  86. }
  87.  
  88. void menu()
  89. {
  90.      int q;
  91.      header();
  92.      printf("\n\nMenu Selection Items:");
  93.      printf("\n\t~[1]Register\n\t~[2]View\n\t~[3]Setup\n\t~[4]Exit\n");
  94.      scanf("%d",&q);
  95.      system("cls");
  96.      switch (q)//C function used to choose a specific operation per input
  97.      {
  98.          case 1: printf("\n\t\t\t||Registration Mode||\n\n");
  99.               regmode();//launch regmode
  100.          break;
  101.          
  102.          case 2: viewmode();//launch viewmode
  103.          break;
  104.          
  105.          case 3: printf("\n\t\t\t||Setup Mode||\n\n");
  106.               setup();//launch setup
  107.          break;
  108.          
  109.          case 4: printf("\n\t\t\tTermination Mode\n\n\t\tGoodbye\n\n");
  110.                  system("pause");
  111.                  exit(0);//closes program
  112.          break;
  113.          
  114.          default: printf("\n\nInvalid Code...Try Again\n\n");
  115.                   system("pause");
  116.          break;//if input is not 1,2,3 or 4....input again
  117.      }
  118. }
  119.  
  120. void regmode()
  121. {
  122.     int fcnt=0,mcnt=0,save;
  123.     header();
  124.     printf("\n How many students are you registering?\n");
  125.         scanf("%d",&numstud);//user inputs the number of students to register and program loops to that number
  126.     for(i=0;i<numstud;i++)
  127.         {                      
  128.                 system("cls");
  129.         header();
  130.                 printf("\n\tStudent # %d\n\n",i+1);
  131.                 printf("Enter Name [Format: First Middle Last] \n");
  132.                 scanf("%s %s %s",&stu[i].fname,&stu[i].midname,&stu[i].lname);
  133.                 printf("\nStudent's Age: ");
  134.                 scanf("%d",&num[i].age);
  135.                 num[i].quu=3;//assign quu a value...if below conditions are met quu value is decreased
  136.                 if(num[i].age>20)
  137.                 {
  138.              num[i].quu-=2;
  139.         }
  140.         if(num[i].age<20 && num[i].age>12)
  141.         {
  142.              num[i].quu-=1;
  143.         }
  144.                 printf("\nGender Male[1],Female[2] ");
  145.                 scanf("%d",&num[i].gen);
  146.                 while(num[i].gen>2||num[i].gen<1)//if statement to propmt user to re enter code if number does not match
  147.                 {
  148.                printf("\nIncorrect input...try again\n");
  149.                scanf("%d",&num[i].gen);
  150.         }
  151.         if(num[i].gen==1)
  152.         {
  153.                strcpy(stu[i].gender,"Male");/*strcpy allows for a string to be coppied into
  154.                                                      into string variable based on input*/
  155.                mcnt=mcnt + 1;//male count to keep record of number of males registered per session
  156.                
  157.         }
  158.         if(num[i].gen==2)
  159.         {
  160.                strcpy(stu[i].gender,"Female");
  161.                fcnt= fcnt + 1;//fcnt: female count to keep record of number of female inputs during registration
  162.         }
  163.         printf("\nCurrent Belt Rank: ");
  164.         scanf("%s",&stu[i].bltrank);
  165.         printf("\nYear Training Comenced: ");
  166.         scanf("%d",&num[i].year);
  167.         system("cls");
  168.         header();
  169.         printf("\n\nIdentification: JKC%d\n",idnum(num[i-1].id));
  170.         printf("\nStudent Name: %s, %s %s",stu[i].lname,stu[i].fname,stu[i].midname);        
  171.         printf("\nStudent's Age: %d",num[i].age);
  172.                 printf("\nQuu: %d",num[i].quu);
  173.                 printf("\nGender: %s",stu[i].gender);
  174.                 printf("\nBelt Rank: %s",stu[i].bltrank);
  175.                 printf("\nYear Training Comenced: %d",num[i].year);
  176.                 printf("\n\nSave:[1]\tStart Over[2]");
  177.                 scanf("\n%d", &save);
  178.                 while(save<1||save>2)
  179.                 {
  180.             printf("\nPlease Choose 1 or 2");
  181.             scanf("%d",&save);
  182.         }
  183.         if(save==1)
  184.                 {//save the data input to a file
  185.             FILE *ptr;
  186.             ptr=fopen("student_data.txt","a+");
  187.             fprintf(ptr,"Identification: JKC%d\n",idnum(num[i].id));
  188.             fprintf(ptr,"\nName: %s %s %s\n",stu[i].lname, stu[i].fname, stu[i].midname);
  189.             fprintf(ptr,"Age: %d\n",num[i].age);
  190.             fprintf(ptr,"Gender: %s\n",stu[i].gender);
  191.             fprintf(ptr,"Belt Rank: %s\n",stu[i].bltrank);
  192.             fprintf(ptr,"QUU Level: %d\n",num[i].quu);
  193.             fprintf(ptr,"Year Training Commenced: %d\n\n",num[i].year);
  194.             fclose(ptr);
  195.             system("cls");
  196.         }
  197.         else if(save ==2)
  198.         {
  199.             system("pause");
  200.             system("cls");
  201.             regmode();
  202.         }
  203.   }
  204.         printf("\n\nRegistration Complete....For Now =>");
  205.         printf("\nTotal Number Of Males Registered: %d",mcnt);
  206.         printf("\nTotal Number Of Females Registered: %d",fcnt);
  207.         printf("\nThank You & Please Enjoy Your Day\n\n");
  208.         FILE *femct;//file pointer to store number of females in system
  209.         femct=fopen("female.txt","r");
  210.         if(femct==NULL)//if the file is empty, the number will simply be added to it.
  211.         {
  212.              femct=fopen("female.txt","w");
  213.              fprintf(femct,"%d",fcnt);
  214.         }
  215.         else
  216.         {
  217.             int fem;
  218.             femct=fopen("female.txt","r");
  219.             fscanf(femct,"%d",&fem);
  220.             fcnt=fcnt+fem;
  221.             femct=fopen("female.txt","w");
  222.             fprintf(femct,"%d",fcnt);
  223.         }
  224.         fclose(femct);
  225.         FILE *memct;//file pointer to store number of males in system
  226.         memct=fopen("male.txt","r");
  227.         if(memct==NULL)//if the file is empty, the number will simply be added to it.
  228.         {
  229.              memct=fopen("male.txt","w");
  230.              fprintf(memct,"%d",mcnt);
  231.         }
  232.         else
  233.         {
  234.             int mem;
  235.             memct=fopen("male.txt","r");
  236.             fscanf(memct,"%d",&mem);
  237.             mcnt=mcnt+mem;
  238.             memct=fopen("male.txt","w");
  239.             fprintf(memct,"%d",mcnt);
  240.         }
  241.         fclose(memct);
  242.         system("pause");
  243.         system("cls");
  244.         menu();  
  245. }
  246.  
  247. void viewmode()
  248. {
  249.      header();
  250.      int vchoice;
  251.      printf("\n\t\t\t||View Mode Options||\n");
  252.      printf("\n~[1]Full Database\n~[2]Search Student\n~[3]Back To Menu\n");
  253.      scanf("%d",&vchoice);
  254.      system("cls");
  255.      switch(vchoice)
  256.      {
  257.           case 1: printf("\n\t\t\t||Full Database||\n\n");
  258.                   char page[size+1];//enables file characters to be stored due to size constant
  259.                   FILE *vptr;//file pointer to: fptr
  260.                   vptr=fopen("student_data.txt","r");//opens file student_data.txt in different pointer in read only mode
  261.                   if(vptr==NULL)
  262.                   {
  263.                        /*"\a" below listed to make terminal beep...
  264.                        this IF statement is to alert user incase there is no file*/
  265.                        printf("\n\n\a\t\tNo File Listed---->Create File First\n");
  266.                        system("pause");
  267.                        system("cls");
  268.                        header();
  269.                        menu();
  270.                   }
  271.                   while(!feof(vptr))
  272.                   {
  273.                        fgets(page,size,vptr);
  274.                        printf("\t%s",page);
  275.                   }
  276.                   system("pause");
  277.                   system("cls");
  278.                   menu();
  279.          break;
  280.          
  281.          case 2: printf("\n\nEnter Last Name:\n");
  282.                  printf("\nComing soon");//incomplete function
  283.                  viewmode();
  284.          break;
  285.          
  286.          case 3: menu();
  287.          break;
  288.          
  289.          default: printf("\n\nInvalid Code...Try Again\n\n");
  290.                   system("pause");
  291.          break;
  292.      }
  293. }
  294.  
  295. int idnum(int x)
  296. {
  297.     int idset = 0 ,d;
  298.     FILE *idrun;
  299.     idrun=fopen("id.txt","r");      
  300.     fscanf(idrun,"%d",&idset);//scans the number in the id.txt into variable.
  301.     d=idset;
  302.     x = 1040+d;
  303.     idset++;
  304.     idrun=fopen("id.txt","w");
  305.     fprintf(idrun,"%d",idset);
  306.     fclose(idrun);    
  307.     return x;
  308. }
  309.  
  310. void setup()
  311. {
  312.      int select;
  313.      header();
  314.      printf("\n\n~[1]New Password\n~[2]I.D. Setup\n~[3]Back To Menu\n");
  315.      scanf("%d",&select);
  316.      system("cls");
  317.      switch(select)
  318.      {
  319.            case 1:
  320.            header();
  321.            char pword[20],pword2[20];
  322.            printf("\n\nNew Password: ");
  323.            scanf("%s",&pword);
  324.            system("cls");
  325.            printf("\nEnter Password Again ");
  326.            scanf("%s",&pword2);
  327.            if(strcmp(pword,pword2)==0)
  328.            {
  329.                 FILE *password;
  330.                 password = fopen("syspass","w");
  331.                 fprintf(password,"%s",pword);
  332.                 fclose(password);
  333.                 system("cls");
  334.                 printf("\nPassword Accepted\n");
  335.                 system("pause");
  336.                 system("cls");
  337.                 menu();
  338.            }
  339.            else
  340.            {
  341.                printf("Inconsistent Data Entered....try again\n");
  342.                system("pause");
  343.                system("cls");
  344.                setup();
  345.            }
  346.            break;
  347.            
  348.            case 2:header();//must be the 1st thing done when system is implimented
  349.                 printf("\nIdentification Code Setup Function\n");
  350.                 int setid;
  351.                 FILE *idcheck;
  352.                 idcheck=fopen("id.txt","r");
  353.                 if(idcheck==NULL)
  354.                 {
  355.                     printf("Identification Code Needed\n Press 1 or 0\n");
  356.                     scanf("%d",&setid);
  357.                     idcheck=fopen("id.txt","w");
  358.                     fprintf(idcheck,"%d",setid);
  359.                     fclose(idcheck);
  360.                 }
  361.                 system("pause");
  362.                 menu();
  363.                 break;
  364.                
  365.            case 3: menu();
  366.            break;
  367.            
  368.            default: printf("\nInvalid Code....Please try Again\n");
  369.            system("pause");
  370.            system("cls");
  371.            setup();
  372.            break;
  373.      }
  374. }