Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to write a program that generates number plates and assigns them to customers. It successfully generates the number plates and are free to be assigned to customers, but during the assigning the program closes abnormally and can't assign in dev c? here is the code so please help me.

C++
//Program to print license number plates e.g UAA 548A
/**********Copy the headers "stdio.h" and "conio.h" in the file****************/
          /* CombinationGenerator.h for use in the makefile while
         "stdlib.h" and "string.h" in the CombiantionAssignment.h */
/*******************************************************************************/


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
  FILE *fp;
  int a,b,c,d,e,f,g,h,i=0;
  int q,p,r,s,x,y,z;
  long int n,sk,wrt,totalcmbs=0;
  char chr[]="UA"; char free[]="Free"; char space[]=" ";
  char ch[1];
  char *name,*combn,*rd1,*rd2,*rd3;
  n=0; q=1;
  x=65; y=0; z=65;
  //clrscr();

  printf("We are generating a pattern of order of this type - UAA 000A \n");
  printf("The pattern has already been chosen in the code as a license number with 3 number and 1 space as defaults \n");

//Prompting user for number of "Numbers" and number of "Spaces" 
  pattern:
  printf("choose an option below: \n");
  printf("1. Numbers 0 to 9 \n");
  printf("2. Spaces \n");
  printf("3. Words - to assign names to license numbers choose this option if the file already exists \n");
  scanf("%d",&a);

  if(a==1)
  {
    printf("How many numbers after the first 3 letters? - 3 is preferred by default \n");
    scanf("%d",&c);
     //if value for numbers has been entered as 3 goto generate-block otherwise prompt again
    if(d==1)
    {
      goto generate;
    }
    goto pattern;
  }

  else if(a==2)
  {
    printf("How many Spaces between the first 3 letters and the remaining letter-number combination?- 1 is deafault \n");
    scanf("%d",&d);
    //if value for spaces has been entered as one go to generate-block otherwise prompt for more entry
     
    if(c==3)
    {
      goto generate;
    }
    goto pattern;
  }

//if file already exists go to assign-block to assign free license numbers in the file to individuals
  else
  {
    goto assign;
  }


/************************* This is the end of main file ***********************************************/
  /* Copy and paste the code above (except headers) in a file and name it Combination.c to be used 
            in a makefile but maintain this original file separate for reference
******************************************************************************************************/


/************************ Beginning of Block for generating the patterns*****************************
     Copy and paste the code below in the file and name it CombinationGenerator.c to used in
                          the makefile
*****************************************************************************************************/                                    

  generate:

  fp=fopen("file.txt","a+");//open a file named file.txt. if it does not exist it will be created
  goto C;

/* z holds the ASCII value of the characters to be printed at the end of the license number
     combination (A-Z) or 65-90.
 * y holds the value of the 3-digit number to be printed from 0-999
     the preceding zeros are formatted in the fprintf() function.
 * x holds the ASCII value of the characters to be printed at the start after
     the first letters on each part of the license number combination (A-Z) or 65-90. 
*/

//This A-block prints the word "Free" at the end of every license number  
  A:
  if(z<=90)
  {
    fprintf(fp,"%c   Free          \n",z);//write with a newline charater to move to the next line in the file
  }
  z++;
  if(z>90)
  {
    goto B;
  }
  goto C;//goto C to start writing a new combination on a new line

//This B-block prints the 3-digit number
  B:
  if(z>90)
  {
    y++;
    if(y>999)
    {
      goto C;
    }
    z=65;
    goto C;
  }

  if(y<=999)
  {
    fprintf(fp,"%03d",y);
  }
  goto A;

//This C-block prints the first characters e.g "UA" and the "space" on every combination
  C:
  if(x>90)
  {
    fclose(fp);
    goto D;
  }
  if(y!=1000 && z!=91)
  {
    fprintf(fp,"%s%c",chr,x);
    for(g=1;g<=d;g++)
    {
      fputs(" ",fp);
    }
  }

  if(y>999 && z>90)
  {
    x++;
    y=0;
    z=65;
    goto C;
  }
  goto B;

//This D-block prompts the user whether to start assigning names to license numbers generated in the file
  D:
  printf("Choose whether to assign names to numbers or not \n");
  printf("1. yes \n");
  printf("2. no and exit \n");
  scanf("%d",&h);
  if(h==1)
  {
    goto assign;
  }
  printf("press any key to exit \n");
  getch();
  exit(0);
/*********************This is the end of the code for CombinationGenerator.c file**************/




/*****************This is the beginning of the code*******************************************
               for the file named CombinationAssignment.c 
                       till the end of the file
**********************************************************************************************/  
<big>
 assign:
  
  fp=fopen("file.txt","r+");

//loop through the file to count the lenght of each line in the file include the one for the newline character 
  while(fgetc(fp)!=EOF)
  {
    if(ch=="F")
    {
      p=n;//p holds the lenght of the start of the word "Free" in each line in the file
    } 
    
    if(ch=="\n")
    {
      n++;//n holds the value for length of each line in the file
      rewind(fp);
      break;
    }
    n++;
  }

  rewind(fp);//go back to the beginning of the file

//counting the total number of lines in the file(number of license numbers stored)
  while(fgetc(fp)!=EOF)
  {
    if(ch=="\n")
    {
      totalcmbs++;//totalcmbs holds the total number of license numbers (lines) in the file
    }
  }

//Prompt the user to enter names of individuals and numbers to be assigned
  Data:

  printf("Type the name of the indivivual to assign a license number \n");
  scanf("%s",&name);
  printf("Type the corresponding valid license number to be assigned e.g UAA 000A with one space between \n");
  scanf("%s",&combn);

//looping through each line in the generated file to check for a match of the number entered
  while(i<=totalcmbs)
  {
    if(q==1)//q counts the lines in the file
    {
      sk=1;//sk holds the value used for seeking 
    }
    else
    {
      sk=(n*q)+1;//value to use for changing the file pointer so that the right line is read at a time
    }
    fseek(fp,sk,SEEK_SET);
    fscanf(fp,"%s%s%s",rd1,rd2,rd3);//read one line in the file at a time
/* the first group of characters e.g "UAA" is stored in rd1, the next e.g "000A" 
   is stored in rd2 and the last e.g "Free" in rd3
*/
    strcat(rd1,space);//adding a space at the end of the first characters read 
    strcat(rd1,rd2);// combining the fisrt characters to the second with a space between them

//checking if there is a match of the users entered number with what is stored in the file  
    if(strcmp(rd1,combn)==0)
    {
       //if a match is found check if that match is free or not 
       if(strcmp(rd3,free)==0)//free holds the string "Free"
       {
	 rewind(fp);
	 wrt=(n*q)-(n-p);//length for seeking
	 fseek(fp,wrt,SEEK_SET);
	 fprintf(fp,"%s",name);//writing the name of individual entered in place of "Free" alongside the match found
	 fflush(stdin);
	 printf("Do you want to enter more names? \n");
	 printf("1. yes \n");
	 printf("2. no \n");
	 scanf("%d",&s);
	 if(s==1)
	 {
	   goto Data;//go back to start entering more data
	 }
	 break;
      }
      //if no match foung prompt the user again
      else
      {
	printf("The license number you entered is not free \n");
	printf("Do you want to enter another license number? \n");
	printf("1. yes\n");
	printf("2. no and exit\n");
	scanf("%d",&s);
	if(s==1)
	{
	  goto Data;
	}
	else
	{
	  exit(0);//terminate program if user does not want to enter more data
	}
      }
    }
    q++;//increment the number of lines in the file read
    //if no match is found the user entered a wrong license number from what is in the file
    if(q>=totalcmbs)
    {
      printf("The license number you entered is invalid \n");
      break;
    }
    i++;
  }// end of while loop

  fclose(fp);
  printf("press any key to exit \n");
  getch();</big>

/**************************code for CombinationAssignment.c ends here***********************/
}
Posted
Updated 5-Aug-14 22:39pm
v6
Comments
Richard MacCutchan 5-Aug-14 6:48am    
Please use <pre> tags around your code to make it readable, and indicate what the error is and where it occurs.
Richard MacCutchan 5-Aug-14 6:50am    
And with that many goto statements you are bound to have problems.
Ssekajjugo Edward 6-Aug-14 4:03am    
Richard thanks bug thought goto would be easy
Richard MacCutchan 6-Aug-14 4:14am    
Yes goto is easy, it is also a quick route to lots of bugs in your code. Learn to use proper structured code.
[no name] 5-Aug-14 7:13am    
Do you always scream at people that you expect to get help from?

1 solution

So to make it official:
In Data section:
CHANGE:

C++
while (i <= totalcmbs)


TO:

C++
while (i < totalcmbs)


ALSO: (with regards to the next issue you posted)
In Main function:
CHANGE:

C++
else if(a==2)
{
  printf("How many Spaces between the first 3 letters and the remaining letter-number combination?- 1 is deafault \n");
  scanf("%d",&d);
  //if value for spaces has been entered as one go to generate-block otherwise prompt for more entry
     
  if(c==3)
  {
    goto generate;
  }
  goto pattern;
}


TO:

C++
else if(a==2)
{
  printf("How many Spaces between the first 3 letters and the remaining letter-number combination?- 1 is deafault \n");
  scanf("%d",&c);  // NOTE I changed the 'd' to a 'c'
  //if value for spaces has been entered as one go to generate-block otherwise prompt for more entry
     
  if(c==3)
  {
    goto generate;
  }
  goto pattern;
}


ALSO:
In Main function:
CHANGE:
C++
if(a==1)
{
  printf("How many numbers after the first 3 letters? - 3 is preferred by default \n");
  scanf("%d",&c);
   //if value for numbers has been entered as 3 goto generate-block otherwise prompt again
  if(d==1)
  {
    goto generate;
  }
  goto pattern;
}


TO:

C++
if(a==1)
{
  printf("How many numbers after the first 3 letters? - 3 is preferred by default \n");
  scanf("%d",&d); // NOTE: Changed from 'c' to 'd'
  //if value for numbers has been entered as 3 goto generate-block otherwise prompt again
  if(d==1)
  {
    goto generate;
  }

  goto pattern;
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900