Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#define limitation 9999
FILE *fpt;
char group;
int tmpPart;
char option;
int counter;
int Min_age;
int Max_age;
int main_menu(void);
int registration(void);
int report(void);
void register_new();
void modify_current();
char end_input = 'n';
int no_record = 1000;
int i = 1000;
char cont= 'y';
char choice= 'n';
int search;
char status ='n';
delete_participant(void);
int temp;
int y;
char age_group;
void report_single();
void report_all();
int d;
struct reg {
int id;
char name[20];
char icno[14];
int age;
} participant[50];
void main()
{
clrscr();
main_menu();
getch();
}
int main_menu(void)
{
char option;
do {
clrscr();
printf("***************************************************************\n");
printf("* *\n");
printf("*Welcome To The Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* *\n");
printf("* M a i n M e n u *\n");
printf("* *\n");
printf("***************************************************************\n\n");
printf(" 1) Registration of Participant\n\n");
printf(" 2) Report Generator\n\n");
printf(" 3) Exit\n\n");
printf(" Please enter your choice ( 1 - 3 ) ->>");
option= getche();
switch(option) {
case '1' :
registration();
break;
case '2' :
report();
break;
case '3' :
printf("\n\nThanks for Using IT Marathon System\n");
printf("System Terminated\n");
exit(0);
default :
printf("\n\nINPUT ERROR!!! PLEASE TRY AGAIN");
printf("\n Press Any Key To Continue");
getch();
main_menu();
}
}
while(option != '3');
return 0;
}
int registration(void)
{
char option;
do {
clrscr();
printf("***************************************************************\n");
printf("* *\n");
printf("*Welcome To The Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* *\n");
printf("* R e g i s t r a t i o n o f P a r t i c i p a n t *\n");
printf("* *\n");
printf("***************************************************************\n\n");
printf(" 1) Registration of New Participant\n\n");
printf(" 2) Modify Current Participant's Information\n\n");
printf(" 3) Deletion of Participant\n\n");
printf(" 4) Exit to Main Menu\n\n");
printf(" Please enter your choice ( 1 - 4 ) ->>");
option= getche();
switch(option) {
case '1' :
register_new();
break;
case '2' :
modify_current();
break;
case '3' :
delete_participant();
break;
case '4' :
main_menu();
break;
default :
printf("\n\nINPUT ERROR!!! PlEASE TRY AGAIN");
printf("\n Press Any Key To Continue");
getch();
registration();
}
} while(option != '5'); return(option);
}
int report(void)
{
char option;
do {
clrscr();
printf("***************************************************************\n");
printf("* *\n");
printf("*Welcome To The Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* *\n");
printf("* R e p o r t G e n e r a t o r *\n");
printf("* *\n");
printf("***************************************************************\n\n");
printf(" 1) Report on Single Age Group\n\n");
printf(" 2) Report on All Participants (Sorted on Age)\n\n");
printf(" 3) Exit to Main Menu\n\n");
printf(" Please enter your choice ( 1 - 3 ) ->>");
option= getche();
switch(option) {
case '1' :
report_single();
break;
case '2' :
report_all();
break;
case '3' :
main_menu();
break;
default :
printf("\n\nINPUT ERROR!!! PlEASE TRY AGAIN");
printf("\n Press Any Key To Continue");
getch();
report();
}
} while(option != '3'); return(option);
}
void register_new()
{
clrscr();
fpt=fopen("register.dat","w+");
printf("***************************************************************\n");
printf("* *\n");
printf("* Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* *\n");
printf("* Registration of New Participant *\n");
printf("* *\n");
printf("***************************************************************\n");
if(no_record >= limitation)
{
printf(" The record have reached the limitation!!!\n");
printf(" Please press any key to exit\n");
getch();
end_input = 'n';
}
while (end_input == 'y')
{
fflush(stdin);
i = no_record;
printf("Index number : %d\n",++i, &participant[i].id);
fflush(stdin);
printf("Name : ");
scanf("%s", &participant[i].name);
fflush(stdin);
printf("IC No. : ");
scanf("%s", &participant[i].icno);
fflush(stdin);
printf("Age : ");
scanf("%d", &participant[i].age);
fflush(stdin);
no_record = no_record + 1;
printf("\nContinue with another record? ( Y/N )\n");
scanf("%c", &end_input);
fflush(stdin);
while (!(end_input == 'y' || end_input == 'n')) {
printf("\n\nINPUT ERROR!!! PlEASE TRY AGAIN");
printf("\n Press Any Key To Continue");
getch();
printf("\nContinue with another record? ( Y/N ) :\n");
scanf("%c", &end_input);
}
}
fwrite(participant, sizeof(struct reg), no_record, fpt);
fclose(fpt);
}
void modify_current()
{
clrscr();
fpt=fopen ("register.dat","r+");
printf("***************************************************************\n");
printf("* *\n");
printf("* Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* *\n");
printf("* Modify Current Participant' s Information *\n");
printf("* *\n");
printf("***************************************************************\n");
while (cont == 'y') {
printf("\nPlease Enter The Participant's Index Number To Be Modify");
printf("\nIndex number : ");
scanf("%d", &search);
printf("\n");
for (i = 1000; i < no_record; i++)
if (search == participant[i].id)
{
printf("\nIndex number : %d\n", participant[i].id);
printf(" 1) Name : %[^\n]\n", participant[i].name);
printf(" 2) IC No. : %s\n", participant[i].icno);
printf(" 3) Age : %d\n", participant[i].age);
printf("\nPlease enter number for modification :\n");
option= getche();
switch(option) {
case '1' :
printf("Name : ");
scanf("%s", &participant[i].name);
break;
case '2' :
printf("IC No. : ");
scanf("%s", &participant[i].icno);
break;
case '3' :
printf("Age : ");
scanf("%d", &participant[i].age);
break;
default :
printf("\n\nINPUT ERROR!!! PlEASE TRY AGAIN");
printf("\n Press Any Key To Continue");
getch();
printf("\nPlease enter number for modification :\n");
option= getche();
} status ='y';
printf("Save the changes or Discard changes? (Y/N)");
scanf("%c", &choice);
while (!(choice == 'y' || choice == 'n')) {
printf("\n\nINPUT ERROR!!! PlEASE TRY AGAIN");
printf("\n Press Any Key To Continue");
getch();
printf("Save the changes or Discard changes? (Y/N)");
scanf("%c", &choice);
}
}
if (status != 'y')
printf(" The Index Number Not Found\n");
status = 'n';
printf("\n");
fflush(stdin);
while (choice == 'y') {
fwrite(participant,sizeof(struct reg), 1, fpt);
}
printf("Continue ??? ( Y / N ) : ");
scanf("%c", & cont);
}
fclose(fpt);
}
delete_participant(void)
{
clrscr();
fpt = fopen("register.dat","r+");
fread(participant, sizeof(struct reg), no_record, fpt);
printf("***************************************************************\n");
printf("* *\n");
printf("* Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* *\n");
printf("* Deletion of Participant *\n");
printf("* *\n");
printf("***************************************************************\n");
while (cont== 'y')
{
clrscr();
printf("\nPlease Enter The Participant Index Number To Be Delete");
printf("\nIndex Number : ");
scanf("%d",&search);
printf("\n");
for (i = 1001; i <= no_record; i++)
{
if (search == participant[i].id)
{
printf("\nIndex number : %d\n", participant[i].id);
printf(" 1) Name : %[^\n]\n", participant[i].name);
printf(" 2) IC No. : %s\n", participant[i].icno);
printf(" 3) Age : %d\n", participant[i].age);
printf("Delete This Record ??? (Y/N)\n");
scanf("%c", &choice);
while (choice == 'y')
{
temp = i;
for (y=1001 ;y <=( no_record - i );y++)
{
participant[temp].id = participant[temp+y].id;
strcpy(participant[temp].name,participant[temp+y].name);
strcpy(participant[temp].icno,participant[temp+y].icno);
participant[temp].age = participant[temp+y].age;
status ='y';
temp = temp + 1;
printf("\n%d",participant[temp].id);
printf("\n%d",y);
getch();
}
}
}
}
if (status !='y')
printf(" The Index Number Not Found\n");
printf("Please Try Other Index Number\n");
status='n';
printf("\n");
fflush (stdin);
printf("Continue y/n : ");
scanf("%c",&cont);
}
fwrite(participant, sizeof(struct reg), no_record - 1, fpt);
fclose(fpt);
return(cont);
}
void report_single()
{
counter = 0;
Min_age = -1;
Max_age = -1;
clrscr();
fpt=fopen("register.dat","r");
fread(participant,sizeof(struct reg),no_record,fpt);
printf("***************************************************************\n");
printf("* *\n");
printf("* Naturally Healthy Pte Ltd IT Marathon System *\n");
printf("* ----Set Out Strong For IT Awareness---- *\n");
printf("* Report On Single Age Group *\n");
printf("* *\n");
printf("***************************************************************\n");
printf(" **********************************************");
printf("\n | Report On Single Member Group |\n");
printf(" ----------------------------------------------");
printf("\n | Enter the Age Group: |" );
printf("\n | Valid Age Group [A,B,C, or D] |\n");
printf(" **********************************************");
gotoxy(45,11);
fflush(stdin);
scanf("%c",&age_group);
fflush(stdin);
while (!(age_group == 'A' || age_group == 'B' || age_group== 'C' || age_group == 'D'))
{
printf("Input Error!!! Please Try Again\n");
printf("Press Any Key To Continue\n");
getch();
printf("Enter The Age Group : \n");
printf("Valid Age Group (A, B, C or D)\n");
scanf("%c",&age_group);
fflush(stdin);
}
switch(group)
{
case 'A': Min_age = 18;
Max_age = 29;
break;
case 'B': Min_age = 30;
Max_age = 49;
break;
case 'C': Min_age = 50;
Max_age = 59;
break;
case 'D': Min_age = 60;
Max_age = 70;
break;
}
clrscr();
printf("\n IT MARATHON");
printf("\n Report On Age Group %c",age_group);
printf("\n--------------------------------------------------------------------------------");
printf("ID\t\tName\t\t\t\tICNo\t\t\tAge");
printf("\n--------------------------------------------------------------------------------");
for(int i=1000; i< no_record; i++)
{
if(participant[i].age >= Min_age && participant[i].age <= Max_age)
{
printf("\n%d \t\t %s \t\t\t\t %s \t\t\t %d",
participant[i].id,
participant[i].name,
participant[i].icno,
participant[i].age);
counter++;
}
}
if (counter==0)
{
printf("\nThere Are No Participants For This Age Group\n");
}
else
{
printf("\n\nTotal Number Of Participants For Group %c",age_group );
printf("%d",counter);
printf("\n--------------------------------------------------------------------------------");
}
char print_rec = 'N';
printf("\n\nDo you wish to print?[Y/N] :");
scanf("%c", &print_rec);
fflush(stdin);
}
void report_all()
{
int counter = 0;
clrscr();
fpt=fopen("register.dat","r");
fread(participant,sizeof(struct reg),no_record,fpt);
reg tmpPart;
//sort the array by age
for(int i = 1000; i< no_record; i++ )
{
int small_age = 9999; //initialize to largest value
int small_id = -1; //initialize
for(int d = i; d < no_record; d++ )
{
if(small_age > participant[d].age)
{
small_age = participant[d].age;
small_id = d;
}
}
//if found smallest index
if(small_id > i)
{
tmpPart.id = participant[i].id;
strcpy(tmpPart.name, participant[i].name);
strcpy(tmpPart.icno, participant[i].icno);
tmpPart.age = participant[i].age;
participant[i].id = participant[small_id].id;
strcpy(participant[i].name, participant[small_id].name);
strcpy(participant[i].icno, participant[small_id].icno);
participant[i].age = participant[small_id].age;
participant[small_id].id = tmpPart.id;
strcpy(participant[small_id].name, tmpPart.name);
strcpy(participant[small_id].icno, tmpPart.icno);
participant[small_id].age = tmpPart.age;
}
}
clrscr();
printf("\n IT MARATHON");
printf("\n Report On All Participants");
printf("\n--------------------------------------------------------------------------------");
printf("ID\t\tName\t\t\t\tICNo\t\t\tAge");
printf("\n--------------------------------------------------------------------------------\n");
for(int all = 1000; all < no_record; all++ )
{
printf("%d \t\t %s \t\t\t\t %s \t\t\t %d\n", participant[all].id, participant[all].name,
participant[all].icno, participant[all].age);
counter++;
}
printf("\nTotal Number Of Participants : %d",counter);
printf("\n--------------------------------------------------------------------------------");
char print_rec = 'N';
printf("\nDo you wish to print?[Y/N] :");
scanf("%c", &print_rec);
fflush(stdin);
}
[mod edit - codetags in future please!]