C Code Please Help

This is a discussion on C Code Please Help within the C Programming forums, part of the General Programming Boards category; Hi.. ive been working on a code for the past week now and ive stumbled upon a problem.....the code is ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    C Code Please Help

    Hi.. ive been working on a code for the past week now and ive stumbled upon a problem.....the code is about a Vehicle Parking System where u enter cars give them spaces and then remove them when exiting....just like a parking system...however the problem i have is in Exiting... after i try to exit a vehicle the program crashes..can someone give me some assistance on how to correct this problem plzzz........i use the Codeblock program to write it if tht helps


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #define CAR 1     //NORMAL CARS
    #define CAR2 2  //RESERVED CARS ONLY
    
    /* to store vehicle number, and its   row-col position in an array */struct vehicle
    {
        int num ;
        int row ;
        int col ;
        int type ;
    } ;
    int parkinfo[4][10] ;  /* a 2-D array to store number of vehicle parked */int vehcount ;  /* to store total count of vehicles */int carcount ;    /* stores total count of cars */int car2count ;  /* stores total count of car2 */void display( ) ;
    void changecol ( struct vehicle * ) ;
    struct vehicle * add ( int, int, int, int ) ;
    void del ( struct vehicle *v ) ;
    void getfreerowcol ( int, int * ) ;
    void getrcbyinfo ( int, int, int * ) ;
    void show( ) ;
    /* decrements the col. number by one   this fun. is called when the data is   shifted one place to left */void changecol ( struct vehicle *v )
    {
        v -> col = v -> col - 1 ;
    }
    /* adds a data of vehicle */struct vehicle * add ( int t, int num, int row, int col )
    {
        struct vehicle *v ;
        v = ( struct vehicle * ) malloc ( sizeof ( struct vehicle ) ) ;
        v -> type = t ;
        v -> row = row ;
        v -> col = col ;
        if ( t == CAR )
               carcount++ ;
        else
            car2count++ ;
        vehcount++ ;
        parkinfo[row][col] = num ;
        return v ;
    }
    /* deletes the data of the specified   car from the array, if found */void del ( struct vehicle *v )
    {
        int c ;
        for ( c = v -> col ; c < 9 ; c++ )
                parkinfo[v -> row][c] = parkinfo[v -> row][c+1] ;
        parkinfo[v -> row][c] = 0 ;
        if ( v -> type == CAR )
            carcount-- ;
        else
            car2count-- ;
        vehcount-- ;
    }
    /* get the row-col position for the vehicle to be parked */void getfreerowcol ( int type, int *arr )
    {
        int r, c, fromrow = 0, torow = 2 ;
        if ( type == CAR2 )
        {
            fromrow += 2 ;
            torow += 2 ;
        }
        for ( r = fromrow ; r < torow ; r++ )
        {
            for ( c = 0 ; c < 10 ; c++ )
            {
                if ( parkinfo[r][c] == 0 )
                {
                    arr[0] = r ;
                    arr[1] = c ;
                    return ;
                }
            }
        }
        if ( r == 2 || r == 4 )
        {
            arr[0] = -1 ;
            arr[1] = -1 ;
        }
    }
    /* get the row-col position for the vehicle with specified number */void getrcbyinfo ( int type, int num, int *arr )
    {
        int r, c, fromrow = 0, torow = 2 ;
        if ( type == CAR2 )
        {
            fromrow += 2 ;
            torow += 2 ;
        }
        for ( r = fromrow ; r < torow ; r++ )
        {
            for ( c = 0 ; c < 10 ; c++ )
            {
                if ( parkinfo[r][c] == num )
                {
                    arr[0] = r ;
                    arr[1] = c ;
                    return ;
                }
            }
        }
        if ( r == 2 || r == 4 )
        {
            arr[0] = -1 ;
            arr[1] = -1 ;
        }
    }
    /* displays list of vehicles parked */void display( )
    {
        int r, c ;
        printf ( "Cars ->\n" ) ;
        for ( r = 0 ; r < 4 ; r++ )
        {
            if ( r == 2 )
                printf ( "Car2 ->\n" ) ;
            for ( c = 0 ; c < 10 ; c++ )
               printf ( "%d\t", parkinfo[r][c] ) ;
            printf ( "\n" ) ;
        }
    }
    void main( )
    {
        int choice, type, number, row = 0, col = 0 ;
        int i, tarr[2] ;
        int finish = 1 ;
        struct vehicle *v ;
        /* creates a 2-D array of car and car2 class */struct vehicle *car[2][10] ;
        struct vehicle *car2[2][10] ;
    
        /* displays menu and calls corresponding functions */while ( finish )
        {
    
            printf ( "\nCar Parking\n" ) ;
            printf ( "1. Arrival of a vehicle\n" ) ;
            printf ( "2. Total no. of vehicles parked\n" ) ;
            printf ( "3. Total no. of cars parked\n" ) ;
            printf ( "4. Total no. of car2 parked\n" ) ;
            printf ( "5. Display order in which vehicles are parked\n" ) ;
            printf ( "6. Departure of vehicle\n" ) ;
            printf ( "7. Exit\n" ) ;
            scanf ( "%d", &choice ) ;
            switch ( choice )
            {
                case  1 :
    
                    printf ( "\nAdd: \n" ) ;
                    type = 0 ;
                    /* check for vehicle type */while ( type != CAR && type != CAR2 )
                    {
                        printf ( "Enter vehicle type (1 for Car / 2 for Car2 ): \n" ) ;
                        scanf ( "%d", &type ) ;
                        if ( type != CAR && type != CAR2 )
                            printf ( "\nInvalid vehicle type.\n" ) ;
                    }
                    printf ( "Enter vehicle licence number: " ) ;
                    scanf ( "%d", &number ) ;
                    /* add cars' data */if ( type == CAR || type == CAR2 )
                    {
                        getfreerowcol ( type, tarr ) ;
                        if ( tarr[0] != -1 && tarr[1] != -1 )
                        {
                            row = tarr[0] ;
                            col = tarr[1] ;
                            if ( type == CAR )
                                car[row][col] =  add ( type, number, row, col ) ;
                            else
                                car2[row - 2][col] = add ( type, number, row, col ) ; ;
                        }
                        else
                        {
                            if ( type == CAR )
                                printf ( "\nNo parking slot free to park a car\n" ) ;
                            else
                                printf ( "\nNo parking slot free to park a car2\n" ) ;
                        }
                    }
                    else
                    {
                        printf ( "Invalid type\n" ) ;
                        break ;
                    }
                    printf ( "\nPress any key to continue..." ) ;
                    getch( ) ;
                    break ;
                case  2 :
    
                    printf ( "Total vehicles parked: %d\n", vehcount ) ;
                    printf ( "\nPress any key to continue..." ) ;
                    getch( ) ;
                    break ;
                case  3 :
                    printf ( "Total cars parked: %d\n", carcount ) ;
                    printf ( "\nPress any key to continue..." ) ;
                    getch( ) ;
                    break ;
                case  4 :
                    printf ( "Total car2 parked: %d\n", car2count ) ;
                    printf ( "\nPress any key to continue..." ) ;
                    getch( ) ;
                    break ;
                case  5 :
    
                    printf ( "Display\n" ) ;
                    display( ) ;
                    printf ( "\nPress any key to continue..." ) ;
                    getch( ) ;
                    break ;
                case  6 :
    
    
                    printf ( "Departure\n" ) ;
                    type = 0 ;
                    /* check for vehicle type */while ( type != CAR && type != CAR2 )
                    {
                        printf ( "Enter vehicle type (1 for Car / 2 for Car2 ): \n" ) ;
                        scanf ( "%d", &type ) ;
                        if ( type != CAR && type != CAR2 )
                            printf ( "\nInvalid vehicle type.\n" ) ;
                    }
                    printf ( "Enter vehicle licence number: "  ) ;
                    scanf ( "%d", &number ) ;
                    if ( type == CAR || type == CAR2 )
                    {
                        getrcbyinfo ( type, number, tarr ) ;
                        if ( tarr[0] != -1 && tarr[1] != -1 )
                        {
                            col = tarr [1] ;
                            /* if the vehicle is car */if ( type == CAR )
                            {
                                row = tarr [0] ;
                                del ( car [row][col] ) ;
                                for ( i = col ; i < 9 ; i++ )
                                {
                                    car[row][i] = car[row][i + 1] ;
                                    changecol ( car[row][i] ) ;
                                }
                                free ( car[row][i] ) ;
                                car[row][i] = NULL ;
                            }
                            /* if a vehicle is car2 */else
                            {
                                row = tarr[0] - 2 ;
                                if ( ! ( row < 0 ) )
                                {
                                    del ( car2[row][col] ) ;
                                    for ( i = col ; i < 9 ; i++ )
                                    {
                                        car2[row][i] = car2[row][i + 1] ;
                                        changecol ( car2[row][col] ) ;
                                    }
                                    car2[row][i] = NULL ;
                                }
                            }
                        }
                        else
                        {
                            if ( type == CAR )
                                printf ( "\nInvalid car number, or a car with such number has not been parked here.\n" ) ;
                            else
                                printf ( "\nInvalid car2 number, or a car2 with such number has not been parked here.\n" ) ;
                        }
                    }
                    printf ( "\nPress any key to continue..." ) ;
                    getch( ) ;
                    break ;
                case  7 :
    
                    for ( row = 0 ; row < 2 ; row++ )
                    {
                        for ( col = 0 ; col < 10 ; col++ )
                        {
                            if ( car[row][col] -> num != 0 )
                                free ( car[row][col] ) ;
                            if ( car2[row][col] -> num != 0 )
                                free ( car2[row+2][col] ) ;
                        }
                    }
                    finish = 0 ;
                    break ;
            }
        }
    }

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I don't want to read all that to find out what you could have just as easily told me.

    What's your problem, and where do you think it might be(5-15 lines please)?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    Quote Originally Posted by Cdre View Post
    i use the Codeblock program to write it if tht helps
    You mean you copied this into Code::Blocks?

    Try writing your own code and you might have a better understanding of what went wrong. Or at least we would be more willing to help you if you wrote the code and then you asked us specific questions about it.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Cheater and hijacker thread closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  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, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21