Thread: Code causing error

  1. #1
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Question Code causing error

    'lo people,

    I have this code, it's a work in progress, that's why bits are missing..

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_VEH 26000
    #define C_SCANF while(getchar() != '\n');
    #define FILENAME "VEHMAIT.DAT"
    #define STR_SIZE sizeof(struct VEH)
    
    
    struct VEH{
       char regno[8];
       char make[21];
       char model[21];
       char chassis[21];
       struct INS_DATE{
          int day;
          int month;
          int year;
          }Ins_date;
       struct NEXT_DATE{
          int day;
          int month;
          int year;
          }Next_date;
       int Ins_freq;
       int brake_freq;
       } veh[MAX_VEH];
    
    
    
    /*Define Functions*/
    
    void C_fgets(char *s_ptr);  /* Removes the '/n' from the end of fgets() input*/
    void Add_veh(FILE *vehicle);
    void Amd_veh(FILE *vehicle);
    void Del_veh(FILE *vehicle);
    void Shw_veh(FILE *vehicle, int pos);
    void Shw_sch(FILE *vehicle);
    void Calc_date();
    int  Calc_pos (char regno[]);
    
    
    int main (void)
    {
    FILE *vehicle;
    
       if ( (vehicle=fopen(FILENAME,"rb+"))== NULL)
            {
              if ( (vehicle=fopen(FILENAME,"wb"))== NULL)
                {
             printf("\nUnable to create key file");
             return 0;
                }
            }
    
    
    
    
    	int option = 0;
    
    	while(option != 5)
                    {
       	clrscr();
       	printf("\n\n\t\t\t Vehicle maintenance program\n");
       	printf("\n\t\t\t 1. Add a vehicle to schedule");
    	printf("\n\t\t\t 2. Amend a vehicles details");
       	printf("\n\t\t\t 3. Delete a vehicle");
    	printf("\n\t\t\t 4. Show current schedule");
       	printf("\n\t\t\t 5. Save changes and exit program");
         	printf("\n\t\t\t >> ");
          scanf("%d", &option);
          C_SCANF;
    
          switch(option)
          	{
             	case 1: Add_veh(vehicle);
                		break;
                case 2: Amd_veh(vehicle);
                		break;
                case 3: Del_veh(vehicle);
                		break;
                case 4: Shw_sch(vehicle);
                		break;
                case 5: printf("\n\t\t\tExiting program");
                       getch();
                		 fclose(vehicle);
                       return 1;
                default: printf("No such option implemented");
    	     	}
    	}
    }
    
    /* Define functions*/
    
    void C_fgets(char *s_ptr)
    {
    char *p = strchr(s_ptr, '\n' );
    if ( p != NULL ) *p = '\0';
    }
    
    void Add_veh(FILE *vehicle)
    {
    
    char regno[8];
    int pos;
    
    clrscr();
    printf("\n\n\t\t\t ADD A VEHICLE\n\n");
    printf("\n VEHICLE DETAILS\n");
    printf("\n Enter vehicle registration number: ");
    fgets(regno,9,stdin);
    pos = Calc_pos(regno);
    strcpy(veh[pos].regno,regno);
    printf("\n Enter vehicle make : ");
    fgets(veh[pos].make,21,stdin);
    printf("\n Enter vehicle model: ");
    fgets(veh[pos].model,21,stdin);
    printf("\n Enter vehicle chassis number: ");
    fgets(veh[pos].chassis,21,stdin);
    printf("\n\n INSPECTION DETAILS\n");
    printf("Enter date of first inspection: ");
    /*Date*/
    printf("Enter frequency of inspections: ");
    scanf("%d", veh[pos].Ins_freq);
    C_SCANF;
    printf("Enter brake check frequency: ");
    scanf("%d", veh[pos].brake_freq);
    C_SCANF;
    
    }
    
    void Amd_veh(FILE *vehicle)
    {}
    
    void Del_veh(FILE *vehicle)
    {}
    
    void Shw_veh(FILE *vehicle, int pos)
    {}
    
    void Shw_sch(FILE *vehicle)
    {}
    
    void Calc_date(FILE *vehicle)
    {}
    
    int Calc_pos(char regno[])
    {
    	int pos;
    	pos = regno[0] - 'A' *1000+
    	   regno[1] - '0' *100+
       	   regno[2] - '0' *10+
          	   regno[3] - '0' *1;
    	return pos;
    }
    Thing is, when I run it, after typing in the reg the program crashes, with an access violation.

    Any help, random abuse, kind words, latin quotations... all are welcome...
    Last edited by Inept Pig; 11-11-2002 at 10:51 AM.
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    fgets(regno,9,stdin); -> you only have space for 8.

    scanf("%d", &veh[pos].Ins_freq);
    scanf("%d", &veh[pos].brake_freq);

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    scanf("%d", veh[pos].Ins_freq);
    C_SCANF;
    Scanf is not a safe function to read from stdin. You can use "safer" functions to do this (fgets for example):
    Code:
    char buf[BUFSIZ];
    
    if(fgets(buf, BUFSIZ, stdin) == NULL)
       return -1;
    
    if(sscanf(buf, "%d", &veh[pos].Ins_freq) != 1)
       return -2;

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by vVv
    > Scanf is not a safe function to read from stdin.

    Actually, it can be. Depending on how good the caller knows how make an apropriate format string.

    Code:
    char buf[128];
    if( scanf( "%128[^\n]%*[^\n]", buf ) != EOF ) {
      getchar( );
    }
    And you don't because this function is still not safe.

  5. #5
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Okay, but besides scanf being the downfall of humanity, is there any other reason why my program would crash?
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Inept Pig
    Okay, but besides scanf being the downfall of humanity, is there any other reason why my program would crash?
    >pos = Calc_pos(regno);
    >strcpy(veh[pos].regno,regno);
    print the value of pos, and see if it is within the array bounds. Also, as already stated you have confusion between 8 and 9 on the array sizes.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    I've done the 8/9 thingy, I'll stop making stupid mistakes one day.... hopefully

    Shall give your other recommendation a go, thanks for the advice
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by vVv
    > And you don't because this function is still not safe.

    Elaborate, please.
    You already changed it. And something else, what if the scanf function returns 0?

    Originally posted by vVv
    > Actually, it can be. Depending on how good the caller knows how make an apropriate format string.
    Okay, I was wrong about the "not safe" part, but still I prever the fgets function to read from input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM