Help my program is giving a problem

This is a discussion on Help my program is giving a problem within the C Programming forums, part of the General Programming Boards category; Hey Tater thx for try with me eve nthough most of the time i have no idea what your talking ...

  1. #61
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Hey Tater thx for try with me eve nthough most of the time i have no idea what your talking about but at number 5. why would i do that could u pls explain that part

  2. #62
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zangetsu View Post
    Hey Tater thx for try with me eve nthough most of the time i have no idea what your talking about but at number 5. why would i do that could u pls explain that part
    Looking at your code I was of the impression you had a gate fee... a flat rate for the first 30 minutes... So that's what that was about...

    Anyway... here it is in 49 lines... AND... it accounts for passing midnight, passing new years, leap years, different rates for cars and trucks and even long stays... (It's not properly error trapped for stuff like exit before enter or invalid dates and times... but it's working here.)

    Code:
    // CITY PARKING
    #include <stdio.h>
    #include <time.h>
    // monetary values
    #define PRICE_CAR    1.25      // car rate per hour
    #define PRICE_TRUCK  2.50      // truck rate per hour 
    #define GATE_CHARGE  5.00      // flat rate for flat rate period
    #define GATE_TIME    30        // length of  flat rate period
    #define SALES_TAX    17.5      // tax addon in percent
    
    int _cdecl main( void )
      { int tOnLot = 0;
        float Rate = PRICE_CAR; 
        printf("\n\t\t *****  CITY PARKING *****\n\n");
        // get time in and time out
        { struct tm tIn = {0}, tOut = {0}; 
          printf("Enter dates and times numerically as YYYY MM DD HH MM\n\n");
          printf("Time In  :\t");
          scanf("%d %d %d %d %d",&tIn.tm_year,&tIn.tm_mon,&tIn.tm_mday,
                                  &tIn.tm_hour,&tIn.tm_min);
          tIn.tm_year -= 1900;
          printf("Time Out :\t");
          scanf("%d %d %d %d %d",&tOut.tm_year,&tOut.tm_mon,&tOut.tm_mday,
                                  &tOut.tm_hour,&tOut.tm_min);
          tOut.tm_year -= 1900;
          tOnLot = (mktime(&tOut) - mktime(&tIn)) / 60; }  // time in minutes  
        // get vehicle type and set hourly rate
        { getchar();
          printf("\nIs this a Truck? (Y or N) : ");
          int vt = getchar();
          if ( vt == 'y' || vt == 'Y')
            Rate = PRICE_TRUCK; }   
        // display time
        printf("\nTime on Lot : %d minutes\n\n",tOnLot);    
        // calculate billable time
        { float Total = 0;
          float Time = 0;
          float Tax = 0;
          tOnLot -= GATE_TIME;
          printf("Gate Fee  :\t%8.2f\n",GATE_CHARGE); 
          if( tOnLot > 0)
            { Time = (tOnLot * Rate) / 60.0;
              printf("Time      :\t%8.2f   (%dmin @ $%.2fhr) \n",Time,tOnLot,Rate); }
          Total = GATE_CHARGE + Time;
          printf("SubTotal  :\t%8.2f\n", Total);
          Tax = Total * (SALES_TAX / 100.0);
          printf("Sales Tax :\t%8.2f\n\n",Tax);
          Total += Tax;
          printf("Total Due :\t%8.2f\n\n",Total); }   
        return 0; }
    Last edited by CommonTater; 02-27-2011 at 11:36 PM. Reason: Added variable flat rate period (GATE_TIME)

  3. #63
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Thanks for taking the time out to show me an example of what u were talking about i'll try and emulate the tax part into my program but i'm not going to put in the year, month and day part cause i wouldn know how to error check for the different days of the month like e.g how february only has 28 days. but what is important in my assignment is that my function are split and are not into one big main function.

  4. #64
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zangetsu View Post
    Thanks for taking the time out to show me an example of what u were talking about i'll try and emulate the tax part into my program but i'm not going to put in the year, month and day part cause i wouldn know how to error check for the different days of the month like e.g how february only has 28 days. but what is important in my assignment is that my function are split and are not into one big main function.
    1) The Time library should automatically know things like days in months, leap years, dayligh savings etc. That's what it's about... you give it a year, month, day, hour, minute, second and it converts that into the number of seconds since midnight, January 1, 1970. Then all time functions have a common reference... You can then add, subtract, etc. The only real error check you should need is if tOnLot ends up a negative number.

    2) If you look closely at the way I wrote that code you'll find "extra" brackets in there. These are scopes that could easily be broken out into their own functions.

    But I agree you should not hand in my example... if your prof does any searching, you're busted! You do need to rewrite your own version. The point was to show you how easily it can be done when you convert the time to seconds or minutes, as appropriate.

  5. #65
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    when i run some tests on your code i took out year month and day and it didnt work i actually got the same number that i use to get from my previous code.lol the 58.75
    this is what i modified with your code

    Code:
    // CITY PARKING
    #include <stdio.h>
    #include <time.h>
    // monetary values
    #define PRICE_CAR    50.25      // car rate per hour
    #define PRICE_TRUCK  100.50      // truck rate per hour
    #define GATE_CHARGE  50.00      // flat rate for flat rate period
    #define GATE_TIME    30        // length of  flat rate period
    #define SALES_TAX    17.5      // tax addon in percent
    
    int _cdecl main( void )
      { int tOnLot = 0;
        float Rate = PRICE_CAR;
        printf("\n\t\t *****  CITY PARKING *****\n\n");
        // get time in and time out
        { struct tm tIn = {0}, tOut = {0};
          printf("Enter dates and times numerically as HH MM\n\n");
          printf("Time In _________________________________\n");
    
          printf("Hour:\n");
          scanf("%d",&tIn.tm_hour);
    
        while((tIn.tm_hour<0 || tIn.tm_hour>24))
      {
        printf("\a\a\a Please Input Correct Hour!\n");
        scanf("%d",& tIn.tm_hour);
      }
          printf("Mintues:\n");
          scanf("%d",&tIn.tm_min);
    
        while((tIn.tm_min<0 || tIn.tm_min>59))
      {
        printf("\a\a\a Please Input Correct Minutes!\n");
        scanf("%d",& tIn.tm_min);
      }
    
          printf("Time Out _________________________________\n");
    
          printf("Hour:\n");
          scanf("%d",&tOut.tm_hour);
    
        while((tOut.tm_hour<0 || tOut.tm_hour>24))
      {
        printf("\a\a\a Please Input Correct Hour!\n");
        scanf("%d",& tOut.tm_hour);
      }
          printf("Mintues:\n");
          scanf("%d",&tOut.tm_min);
    
        while((tOut.tm_min<0 || tOut.tm_min>59))
      {
        printf("\a\a\a Please Input Correct Minutes!\n");
        scanf("%d",& tOut.tm_min);
      }
    
          tOnLot = (mktime(&tOut) - mktime(&tIn)) / 60; }  // time in minutes
        // get vehicle type and set hourly rate
        { getchar();
          printf("\nIs this a Truck? (Y or N) : ");
          int vt = getchar();
          if ( vt == 'y' || vt == 'Y')
            Rate = PRICE_TRUCK; }
        // display time
        printf("\nTime on Lot : %d minutes\n\n",tOnLot);
        // calculate billable time
        {
          float Total = 0;
          float Time = 0;
          float Tax = 0;
          tOnLot -= GATE_TIME;
          printf("Gate Fee  :\t%8.2f\n",GATE_CHARGE);
          if( tOnLot > 0)
            {
              Time = (tOnLot * Rate) / 60.0;
              printf("Time      :\t%8.2f   (%dmin @ $%.2fhr) \n",Time,tOnLot,Rate);
            }
          Total = GATE_CHARGE + Time;
          printf("SubTotal  :\t%8.2f\n", Total);
          Tax = Total * (SALES_TAX / 100.0);
          printf("Sales Tax :\t%8.2f\n\n",Tax);
          Total += Tax;
          printf("Total Due :\t%8.2f\n\n",Total);
          }
        return 0;
        }

  6. #66
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's right... the time library works on the number of seconds since midnight January 1, 1970... you're giving it year 0, month 0, day 0 ...

    Moreover your mini-version fails to account for crossing midnight, month end, year end etc.

    If you don't care about these things don't use time ... just take (hour * 60 ) + minutes and subtract.
    Last edited by CommonTater; 03-01-2011 at 12:32 PM.

  7. #67
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Thx its done an it works i just need two more functions now anybody have any ideas of what i could add to this apart from allocating or keeping track of parking space cause that sounds hella difficult.lol

  8. #68
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    my program is basically finished now i jus need some error checking in there but don't know how i tried the isalpha thing for my menu its below scanf for choice but it doesn't work

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <ctype.h>
    
    #define CAR 1
    #define TRUCK 2
    
    /* to store vehicle number, and its
       row-column position in an array */
    struct vehicle
    {
        int num;
        int row;
        int column;
        int type;
    };
    int isdigit( int ch );
    int isalpha( int ch );
    int parking_lot[4][10];  /* a 2-D array to store number of vehicle parked */
    int V_count;  /* to store total count of vehicles */
    int carcount;    /* stores total count of cars */
    int truckcount;  /* stores total count of trucks */
    
    void display();
    void changecolumn(struct vehicle*);
    struct vehicle * add(int, int, int, int);
    void delete_func(struct vehicle*);
    void getfreerowcolumn(int, int*);
    void get_info(int, int, int*);
    void show();
    
    /* decrements the column. number by one
       this fun. is called when the data is
       shifted one place to left */
    void changecolumn(struct vehicle *P)
    {
        P -> column = P -> column-1;
    }
    
    /* adds a data of vehicle */
    struct vehicle* add(int t, int num, int row, int column)
    {
        struct vehicle *P;
    
        P=(struct vehicle*) malloc(sizeof(struct vehicle));
    
        P -> type=t;
        P -> row=row;
        P -> column=column;
    
        if(t==CAR)
               carcount++;
        else
               truckcount++;
    
        V_count++;
        parking_lot[row][column]=num;
    
        return P;
    }
    
    /* deletes the data of the specified
       car from the array, if found */
    void delete_func(struct vehicle *P)
    {
        int c;
    
        for (c=P -> column;c<9;c++)
                parking_lot[P -> row][c] = parking_lot[P -> row][c+1];
    
        parking_lot[P -> row][c]=0;
    
        if(P -> type == CAR)
            carcount--;
        else
            truckcount--;
    
            V_count--;
    }
    
    /* get the row-column position for the vehicle to be parked */
    void getfreerowcolumn (int type, int *arr)
    {
        int r, c, fromrow = 0, torow = 2;
    
        if(type == TRUCK)
        {
            fromrow += 2;
            torow += 2;
        }
    
        for(r=fromrow;r<torow;r++)
        {
            for(c=0;c<10;c++)
            {
                if(parking_lot[r][c]==0)
                {
                    arr[0] = r;
                    arr[1] = c;
                    return 0;
                }
            }
        }
    
        if(r == 2 || r == 4)
        {
            arr[0] = -1;
            arr[1] = -1;
        }
    }
    
    /* get the row-column position for the vehicle with specified number */
    void get_info (int type, int num, int *arr)
    {
        int r, c, fromrow = 0, torow = 2;
    
        if(type == TRUCK)
        {
            fromrow += 2;
            torow += 2;
        }
    
        for(r=fromrow;r<torow;r++)
        {
            for(c=0;c<10;c++)
            {
                if(parking_lot[r][c] == num)
                {
                    arr[0] = r;
                    arr[1] = c;
                    return 0;
                }
            }
        }
    
        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("Trucks ->\n");
    
            for(c=0;c<10;c++)
               printf("%d\t",parking_lot[r][c]);
               printf("\n");
        }
    }
    
    void main()
    {
        int choice, type, number, row = 0, column = 0;
        int i, tarr[2];
        int finish = 1;
        int hrsn=0; //Hour the vehicle entered the parking lot
        int minn=0; //Minute the vehicle came in the parking lot
        int hrso=0; //Hour the vehicle left the parking lot
        int mino=0; //Minute the vehicle left the parking lot
        int minutes_max=0;//60 minutes
        int hours_max=0;//24 hours
    
    
        struct vehicle *P;
    
        /* creates a 2-D array of car and truck class */
        struct vehicle *car[2][10];
        struct vehicle *truck[2][10];
    
        system("CLS");
    
        /* displays menu and calls corresponding functions */
        while(finish)
        {
            system("CLS");
            system("color 02");
    
    
        printf("\t\t***************************************\n");
        printf("\t\t***************************************\n");
        printf("\t\t**         Courtney Reid             **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**          6A Business              **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**           Mr. Brown               **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**       Computer Science            **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t***************************************\n");
        printf("\t\t***************************************\n\n\n");
    
    
            printf("\nCar Parking\n");
            printf("1. Arrival of a vehicle\n");
            printf("2. Departure of vehicle\n");
            printf("3. Total number of vehicles parked\n");
            printf("4. Total number of Cars parked\n");
            printf("5. Total number of Trucks parked\n");
            printf("6. Display order in which vehicles are parked\n");
            printf("7. Display Parking Time\n");
            printf("8. Exit\n");
    
            scanf("%d", &choice);
            if(isalpha(choice))
            printf( "You entered a letter of the alphabet\n" );
    
    
    
            switch(choice)
            {
                case  1 :
    
                system("CLS");
    
                printf("What Hour did the vehicle enter the lot? (0-23)\n");
                scanf("%d",&hrsn);
    
                while((hrsn<0 || hrsn>23))
                {
                    printf("\a\a\a Please Input Value from 0-23!\n");
                    scanf("%d",&hrsn);
                }
    
                printf("What Minute did the vehicle enter the lot? (0-59)\n");
                scanf("%d",&minn);
    
                while((minn<0 || minn>59))
                {
                    printf("\a\a\a Please Enter Correct Minute Between 0-59!\n");
                    scanf("%d",&minn);
                }
                printf("Time in %d:%d\n", hrsn, minn);
    
                    printf("\nAdd: \n");
    
                    type = 0;
    
                    /* check for vehicle type */
                    while(type != CAR && type != TRUCK)
                    {
                        printf("Enter vehicle type (1 for Car / 2 for Truck ): \n");
                        scanf("%d", &type);
                        if(type != CAR && type != TRUCK)
                            printf("\nInvalid vehicle type.\n");
                    }
    
                    printf("Enter vehicle number: ");
                    scanf("%d", &number);
                    /* add cars' data */
                    if(type == CAR || type == TRUCK)
                    {
                        getfreerowcolumn(type, tarr);
    
                        if(tarr[0] != -1 && tarr[1] != -1)
                        {
                            row = tarr[0];
                            column = tarr[1];
    
                            if(type == CAR)
                                car[row][column] =  add(type, number, row, column);
                            else
                                truck[row - 2][column] = add(type, number, row, column);
                        }
                        else
                        {
                            if(type == CAR)
                                printf("\nNo parking slot free to park a Car\n");
                            else
                                printf("\nNo parking slot free to park a Truck\n");
                        }
                    }
                    else
                    {
                        printf("Invalid type\n");
                    }
    
                    printf("\nPress any key to continue...");
    
                    getch();
                    break;
    
                case  2 :
    
                system("CLS");
    
                printf("Departure\n");
    
                printf("What Hour did the vehicle leave the lot? (0-23)\n");
                scanf("%d",&hrso);
    
                while((hrso<0 || hrso>23))
                {
                    printf("\a\a\a Please Enter the Correct Time Between 0-23!\n");
                    scanf("%d",&hrso);
                }
    
                printf("What Minute did the vehicle leave the lot? (0-59)\n");
                scanf("%d",&mino);
    
                while(mino<0 || mino>59)
                {
                    printf("\a\a\a PLease Enter the Correct time between 0-59!\n");
                    scanf("%d",&mino);
                }
                printf("Time Out %d:%d\n", hrso, mino);
    
                    type = 0;
    
                    /* check for vehicle type */
                    while(type != CAR && type != TRUCK)
                    {
                        printf("Enter vehicle type 1 for Car or 2 for TRUCK : \n");
                        scanf("%d", &type);
                        if(type != CAR && type != TRUCK)
                            printf("\nInvalid vehicle type.\n");
                    }
                    printf("Enter number: ");
                    scanf("%d", &number);
    
                    if(type == CAR || type == TRUCK)
                    {
                        get_info(type, number, tarr);
                        if(tarr[0] != -1 && tarr[1] != -1)
                        {
                            column = tarr[1];
    
                            /* if the vehicle is car */
                            if(type == CAR)
                            {
                                row=tarr[0]-2;
                                if(!(row<0))
                                {
                                delete_func(car[row][column]);
                                for(i=column;i<9;i++)
                                car[row][i] = car[row][i+1];
                                {
                                    changecolumn(car[row][column]);
                                }
                                car[row][i] = NULL;
                                }
    
                            }
                            /* if a vehicle is truck */
                            else
                            {
                                row = tarr[0]-2;
                                if(!(row<0))
                                {
                                    delete_func(truck[row][column]);
                                    for(i=column;i<9;i++)
                                        truck[row][i] = truck[row][i+1];
                                    {
                                        changecolumn(truck[row][column]);
                                    }
                                    truck[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 truck number, or a truck with such number has not been parked here.\n");
                        }
                    }
    
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case 3 :
    
    
                    system("CLS");
                    printf("Total vehicles parked: %d\n", V_count);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  4 :
    
                    system("CLS");
                    printf("Total cars parked: %d\n", carcount);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  5 :
    
                    system("CLS");
                    printf("Total trucks parked: %d\n", truckcount);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  6 :
    
                    system("CLS");
                    printf("Display\n");
                    display();
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  7 :
    
                    system("CLS");
    
                       if(minn < mino)
                    {
                        minutes_max = mino - minn;
                        //minutes_max = 60 - 5
                        //minutes_max = 55 minutes
                    }
                        else if(minn > mino)
                        {
                            minutes_max = (60 - minn) + mino;
                            hrso = hrso-1;
                        }
                            else
                            {
                                minutes_max = 0; //minutes_in equals minutes_out
                            }
    
            if (hrsn < hrso)
            {
                hours_max = 24 - (hrso - hrsn);
            }
                else if (hrsn > hrso)
                {
                    hours_max = (24 - hrso) + hrsn;
                }
                    else
                    {
                        hours_max = 0;
                    }
    
                    hours_max = (hrso - hrsn);
    
                    printf("Parking Time: %d hours:%d minutes\n", hours_max, minutes_max);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
    
                case  8 :
    
                    system("CLS");
                    exit(1);
                    break;
    
            }
        }
    }

  9. #69
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    choice is an int. You are interpreting the input as an int because scanf has "%d". The case statement checks for 1 - 8. Also correct.

    isalpha() is meant to be used on a character.

  10. #70
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    k most if not all of my input are integers so most of my number inputs crash when i put an char instead of a number i put a default after my last case break but it didn't fix the menu problem

    Code:
     default: system ("CLS"); printf("\n\t\t\tPLEASE SELECT A VALID OPTION");main();

  11. #71
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zangetsu View Post
    k most if not all of my input are integers so most of my number inputs crash when i put an char instead of a number i put a default after my last case break but it didn't fix the menu problem

    Code:
     default: system ("CLS"); printf("\n\t\t\tPLEASE SELECT A VALID OPTION");main();
    NEVER call main() as a function... way too much goes wrong when you do that.

  12. #72
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    If you want to check whether the user typed in letters instead of digits then you must allow any arbitrary chars as input. Not force interpretation by scanf's %d. Use %s and simply capture the string into a char buffer. Then loop through each character to make sure it's '0' to '9 only.

  13. #73
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    tried that an got an error i'm seriously not understanding and i don't see any c examples of error proofing which is really sad

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <ctype.h>
    
    #define CAR 1
    #define TRUCK 2
    
    /* stores vehicle number and its
       row-column position in an array */
    struct vehicle
    {
        int num;
        int row;
        int column;
        int type;
    };
    int isdigit( int ch );
    int isalpha( int ch );
    int parking_lot[4][10];  /* a 2-D array to store number of vehicles parked */
    int V_count;  /* stores total count of vehicles */
    int carcount;    /* stores total count of cars */
    int truckcount;  /* stores total count of trucks */
    
    void display();
    void changecolumn(struct vehicle*);
    struct vehicle * add(int, int, int, int);
    void delete_func(struct vehicle*);
    void getfreerowcolumn(int, int*);
    void get_info(int, int, int*);
    void show();
    
    /* decrements the column. number by one
       this fun. is called when the data is
       shifted one place to left */
    void changecolumn(struct vehicle *P)
    {
        P -> column = P -> column-1;
    }
    
    /* adds a data of vehicle */
    struct vehicle* add(int t, int num, int row, int column)
    {
        struct vehicle *P;
    
        P=(struct vehicle*) malloc(sizeof(struct vehicle));
    
        P -> type=t;
        P -> row=row;
        P -> column=column;
    
        if(t==CAR)
               carcount++;
        else
               truckcount++;
    
        V_count++;
        parking_lot[row][column]=num;
    
        return P;
    }
    
    /* deletes the data of the specified
       car from the array, if found */
    void delete_func(struct vehicle *P)
    {
        int c;
    
        for (c=P -> column;c<9;c++)
                parking_lot[P -> row][c] = parking_lot[P -> row][c+1];
    
        parking_lot[P -> row][c]=0;
    
        if(P -> type == CAR)
            carcount--;
        else
            truckcount--;
    
            V_count--;
    }
    
    /* get the row-column position for the vehicle to be parked */
    void getfreerowcolumn (int type, int *arr)
    {
        int r, c, fromrow = 0, torow = 2;
    
        if(type == TRUCK)
        {
            fromrow += 2;
            torow += 2;
        }
    
        for(r=fromrow;r<torow;r++)
        {
            for(c=0;c<10;c++)
            {
                if(parking_lot[r][c]==0)
                {
                    arr[0] = r;
                    arr[1] = c;
                    return 0;
                }
            }
        }
    
        if(r == 2 || r == 4)
        {
            arr[0] = -1;
            arr[1] = -1;
        }
    }
    
    /* get the row-column position for the vehicle with specified number */
    void get_info (int type, int num, int *arr)
    {
        int r, c, fromrow = 0, torow = 2;
    
        if(type == TRUCK)
        {
            fromrow += 2;
            torow += 2;
        }
    
        for(r=fromrow;r<torow;r++)
        {
            for(c=0;c<10;c++)
            {
                if(parking_lot[r][c] == num)
                {
                    arr[0] = r;
                    arr[1] = c;
                    return 0;
                }
            }
        }
    
        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("Trucks ->\n");
    
            for(c=0;c<10;c++)
               printf("%d\t",parking_lot[r][c]);
               printf("\n");
        }
    }
    
    void main()
    {
        int type, number, row = 0, column = 0;
        int i, tarr[2];
        int finish = 1;
        int hrsn=0; //Hour the vehicle entered the parking lot
        int minn=0; //Minute the vehicle came in the parking lot
        int hrso=0; //Hour the vehicle left the parking lot
        int mino=0; //Minute the vehicle left the parking lot
        int minutes_max=0;//60 minutes
        int hours_max=0;//24 hours
        char* choice;
    
        struct vehicle *P;
    
        /* creates a 2-D array of car and truck class */
        struct vehicle *car[2][10];
        struct vehicle *truck[2][10];
    
        system("CLS");
    
        /* displays menu and calls corresponding functions */
        while(finish)
        {
            system("CLS");
            system("color 02");
    
    
        printf("\t\t***************************************\n");
        printf("\t\t***************************************\n");
        printf("\t\t**         Courtney Reid             **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**          6A Business              **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**           Mr. Brown               **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**       Computer Science            **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t**                                   **\n");
        printf("\t\t***************************************\n");
        printf("\t\t***************************************\n\n\n");
    
    
            printf("\nCar Parking\n");
            printf("1. Arrival of a vehicle\n");
            printf("2. Departure of vehicle\n");
            printf("3. Total number of vehicles parked\n");
            printf("4. Total number of Cars parked\n");
            printf("5. Total number of Trucks parked\n");
            printf("6. Display order in which vehicles are parked\n");
            printf("7. Display Parking Time\n");
            printf("8. Exit\n");
    
            scanf("%s", &choice);
            if(isalpha(choice))
            printf( "You entered a letter of the alphabet\n" );
    
    
    
    
            switch(choice)
            {
                case  1 :
    
                system("CLS");
    
                printf("What Hour did the vehicle enter the lot? (0-23)\n");
                scanf("%d",&hrsn);
    
                while((hrsn<0 || hrsn>23))
                {
                    printf("\a\a\a Please Input Value from 0-23!\n");
                    scanf("%d",&hrsn);
                }
    
                printf("What Minute did the vehicle enter the lot? (0-59)\n");
                scanf("%d",&minn);
    
                while((minn<0 || minn>59))
                {
                    printf("\a\a\a Please Enter Correct Minute Between 0-59!\n");
                    scanf("%d",&minn);
                }
                printf("Time in %d:%d\n", hrsn, minn);
    
                    printf("\nAdd: \n");
    
                    type = 0;
    
                    /* check for vehicle type */
                    while(type != CAR && type != TRUCK)
                    {
                        printf("Enter vehicle type (1 for Car / 2 for Truck ): \n");
                        scanf("%d", &type);
                        if(type != CAR && type != TRUCK)
                            printf("\nInvalid vehicle type.\n");
                    }
    
                    printf("Enter vehicle number: ");
                    scanf("%d", &number);
                    /* add cars' data */
                    if(type == CAR || type == TRUCK)
                    {
                        getfreerowcolumn(type, tarr);
    
                        if(tarr[0] != -1 && tarr[1] != -1)
                        {
                            row = tarr[0];
                            column = tarr[1];
    
                            if(type == CAR)
                                car[row][column] =  add(type, number, row, column);
                            else
                                truck[row - 2][column] = add(type, number, row, column);
                        }
                        else
                        {
                            if(type == CAR)
                                printf("\nNo parking slot free to park a Car\n");
                            else
                                printf("\nNo parking slot free to park a Truck\n");
                        }
                    }
                    else
                    {
                        printf("Invalid type\n");
                    }
    
                    printf("\nPress any key to continue...");
    
                    getch();
                    break;
    
                case  2 :
    
                system("CLS");
    
                printf("Departure\n");
    
                printf("What Hour did the vehicle leave the lot? (0-23)\n");
                scanf("%d",&hrso);
    
                while((hrso<0 || hrso>23))
                {
                    printf("\a\a\a Please Enter the Correct Time Between 0-23!\n");
                    scanf("%d",&hrso);
                }
    
                printf("What Minute did the vehicle leave the lot? (0-59)\n");
                scanf("%d",&mino);
    
                while(mino<0 || mino>59)
                {
                    printf("\a\a\a PLease Enter the Correct time between 0-59!\n");
                    scanf("%d",&mino);
                }
                printf("Time Out %d:%d\n", hrso, mino);
    
                    type = 0;
    
                    /* check for vehicle type */
                    while(type != CAR && type != TRUCK)
                    {
                        printf("Enter vehicle type 1 for Car or 2 for TRUCK : \n");
                        scanf("%d", &type);
                        if(type != CAR && type != TRUCK)
                            printf("\nInvalid vehicle type.\n");
                    }
                    printf("Enter number: ");
                    scanf("%d", &number);
    
                    if(type == CAR || type == TRUCK)
                    {
                        get_info(type, number, tarr);
                        if(tarr[0] != -1 && tarr[1] != -1)
                        {
                            column = tarr[1];
    
                            /* if the vehicle is car */
                            if(type == CAR)
                            {
                                row=tarr[0]-2;
                                if(!(row<0))
                                {
                                delete_func(car[row][column]);
                                for(i=column;i<9;i++)
                                car[row][i] = car[row][i+1];
                                {
                                    changecolumn(car[row][column]);
                                }
                                car[row][i] = NULL;
                                }
    
                            }
                            /* if a vehicle is truck */
                            else
                            {
                                row = tarr[0]-2;
                                if(!(row<0))
                                {
                                    delete_func(truck[row][column]);
                                    for(i=column;i<9;i++)
                                        truck[row][i] = truck[row][i+1];
                                    {
                                        changecolumn(truck[row][column]);
                                    }
                                    truck[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 truck number, or a truck with such number has not been parked here.\n");
                        }
                    }
    
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case 3 :
    
    
                    system("CLS");
                    printf("Total vehicles parked: %d\n", V_count);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  4 :
    
                    system("CLS");
                    printf("Total cars parked: %d\n", carcount);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  5 :
    
                    system("CLS");
                    printf("Total trucks parked: %d\n", truckcount);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  6 :
    
                    system("CLS");
                    printf("Display\n");
                    display();
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
                case  7 :
    
                    system("CLS");
    
                       if(minn < mino)
                    {
                        minutes_max = mino - minn;
                        //minutes_max = 60 - 5
                        //minutes_max = 55 minutes
                    }
                        else if(minn > mino)
                        {
                            minutes_max = (60 - minn) + mino;
                            hrso = hrso-1;
                        }
                            else
                            {
                                minutes_max = 0; //minutes_in equals minutes_out
                            }
    
            if (hrsn < hrso)
            {
                hours_max = 24 - (hrso - hrsn);
            }
                else if (hrsn > hrso)
                {
                    hours_max = (24 - hrso) + hrsn;
                }
                    else
                    {
                        hours_max = 0;
                    }
    
                    hours_max = (hrso - hrsn);
    
                    printf("Parking Time: %d hours:%d minutes\n", hours_max, minutes_max);
                    printf("\nPress any key to continue...");
                    getch();
                    break;
    
    
                case  8 :
    
                    system("CLS");
                    exit(1);
                    break;
    
                default:
    
                    system ("CLS");
                    printf("Incorrect input please try again");
                    getch();
                    break;
            }
        }
    }

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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