Thread: Help!!problem with my code

  1. #1
    Daniel Decosta
    Guest

    Help!!problem with my code

    i'm currently using borland turbo c++ ver 4.5.
    i have just done a problem but the are error in there and i can't solve it(General protection fault) in c programming.
    can u all please help me to check my code
    and what is the printing command for using the printer to print the file record???
    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!]

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Using globals so much is really poor design. Most of the time you'll never ever need to use globals.

    2) Typically, when using #define, you want the definition to stand out. Thus, I prefer the syntax:

    #define INCAPS something

    But that's mainly personal preference.

    3) fflush(stdin) should never be used. It's behaviour is undefined, and it may or may not work as you want it to, depending on the particular compiler you use to compiler your code. Check the FAQ for better solutions. (IE: while(fgetc(stdin)!='\n'); )

    4) Since you use that same huge block of text over and over again, you could save yourself some typing by doing one of two things:

    a) Make it a function:

    Code:
    void displayHeader( void )
    {
        printf(
        " **************************************************
    *************\n"
        "*                                                             *\n"
        "*Welcome To The Naturally Healthy Pte Ltd IT Marathon System  *\n"
        "*          ----Set Out Strong For IT Awareness----            *\n"
        "*                                                             *\n"
        "*            M a i n                     M e n u              *\n"
        "*                                                             *\n"
        " **************************************************"
        );
    }
    Or...

    b) Make it a single string:

    Code:
    const char *textHeader =
        " **************************************************
    *************\n"
        "*                                                             *\n"
        "*Welcome To The Naturally Healthy Pte Ltd IT Marathon System  *\n"
        "*          ----Set Out Strong For IT Awareness----            *\n"
        "*                                                             *\n"
        "*            M a i n                     M e n u              *\n"
        "*                                                             *\n"
        " **************************************************";
    And then when you need it, either do:

    displayHeader( );

    Or...

    printf( textHeader );


    5) Your limitation is 9999, but your array you index has a limit of 50. See the problem?

    6) This is really the same as number 5, but in a different spot:
    Code:
     for (i = 1001; i <= no_record; i++)
    	{
    		if (search == participant[i].id)
    Since 'participant' is an array of 50 structures, can you spot the obvious problem with this for loop? This whole loop is just screwed!

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In addition, you're using elements of C++ in this C code

    Like this
    > for(int i = 1000; i< no_record; i++ )

    And some of your printf/scanf formats are wrong
    cpr-test.c:29: warning: type defaults to `int' in declaration of `delete_participant'
    cpr-test.c:29: warning: data definition has no type or storage class
    cpr-test.c:44: warning: return type of `main' is not `int'
    cpr-test.c: In function `register_new':
    cpr-test.c:200: warning: too many arguments for format
    cpr-test.c:204: warning: char format, different type arg (arg 2)
    cpr-test.c:207: warning: char format, different type arg (arg 2)
    cpr-test.c: In function `modify_current':
    cpr-test.c:253: warning: unknown conversion type character `[' in format
    cpr-test.c:253: warning: too many arguments for format
    cpr-test.c:264: warning: char format, different type arg (arg 2)
    cpr-test.c:268: warning: char format, different type arg (arg 2)
    cpr-test.c: At top level:
    cpr-test.c:314: warning: return-type defaults to `int'
    cpr-test.c: In function `delete_participant':
    cpr-test.c:336: warning: unknown conversion type character `[' in format
    cpr-test.c:336: warning: too many arguments for format
    And main returns int, not void

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    How make the array(participant[50]) to 9999
    i try to change it before but the compiler said the array size is too large.
    Last edited by Daniel decosta; 09-28-2002 at 06:18 AM.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    but i only allow to use borland turbo c++.
    this make me having a lot of trouble.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Modified code(still with errors)

    Please help me to identify and correct the errors.
    Thank you!!!(borland turbo c++ 4.5)

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    People have given you sugguestions but in your modified code you haven't implemented them. Why not do those first before asking people do to more.
    Besides that you should check your fopen to see that a NULL pointer isn't returned.
    Last edited by crag2804; 09-28-2002 at 08:29 AM.
    http://uk.geocities.com/ca_chorltonkids

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    i just learn c programming

    even if i know where is the error
    i also dunno how to correct it.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    i can't seem to access to
    delete_participant and modify_participant module.
    may i know why and please correct it for me.

    there is also error after the index number in the register participant module.

    does anyone know the printing command printing onto the paper?
    Please insert it into the program code under the report on all age and single age and post it back to me.
    thank you!!

    can someone kindly debug and improved the program code for me !!
    thank you!!!
    Last edited by Daniel decosta; 09-28-2002 at 11:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM