Thread: Manual time-c program

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    Manual time-c program

    i having a program that using function that automaticly detect time,date and day.. how can i only want to manually insert time? since the function including those 3 things... below my code

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<time.h>
    #include<stdlib.h>
    #include <string.h>
    #define size 80
    #define MAX 100    
    struct carPark{
        char Car_No[20];
        int parked;
        char d[size];
        char e[size];
    }car[100];
    
    int input();
    void display();
    int timeIn(char [], int, char []);
    int timeOut();
    int CekIn();
    int CekOut();
    int CalculateDuration(int , int, int, int  );
    int save[100][2];
    
    int sd, sm, sy, ed, em, ey,sh, sMin, eh,eM, Duration, Total;
    char a;
    char  days, cont; //Car_no[10],
    double price;
    int n;
    
    int main()
    {
        int option;
        static int c = 0;
        cont:
        for (;;)
        {
        printf("CAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("==================================== \n");
        printf("1 : In\n");
        printf("2 : Out\n");
        printf("0 : Exit\n");
        printf("==================================== \n");
        printf("Your Option : ");
        scanf("%d", &option);
                
            switch(option)
            {
            case 0:
                {
                    printf("Do you really want to exit ? \n(enter 'Y' for exit & enter 'N' for Continue) : ");
                    cont = getchar();
                    scanf("%c", &cont);
                    if ((cont == 'y') || (cont == 'Y'))
                    {
                        return 0;
                    }
                    else if((cont == 'n') || (cont == 'N'))
                    {
                        printf("\nThe system will Continue\n\n");
                        goto cont;
                    }
                }
            case 1:{
                
                if (c>=5)
                {
                    printf("Full Parking \n\n");
                }
                else
                {
                c++;
                printf("\ntotal car : %d\n\n", c);
                CekIn();
                
                }
                break;
            }
            case 2:{
                if (c<=0)
                    {
                    printf("No car parking here \n\n");
                    }
                else{
                    printf("total car : %d\n\n", c);
                    CekOut();
                    c--;
                }
                break;
            }
            default:
            {
                printf("wrong Selection ");
                break;
            }
        }
            system("PAUSE");
            printf("\n");
        }
    }
    int CekIn()
    {
        int x=1;
        //do{
        printf("Enter Your Car Number : ");
        //Car_no = getchar();
        scanf("%s", car[n].Car_No);
        //printf("Days : ");
        //scanf("%s", &days);
        
        //printf("\nDate Of Entry in mm/dd/yyyy format :");
        //scanf("%d/%d/%d", &sd, &sm, &sy);
        //printf("\nTime Of Entry in HH.MM format :");
        //scanf("%d.%d", &sh,&sMin);
        //printf("\nDate Of Exit in mm/dd/yyyy format: ");
        //scanf("%d/%d/%d", &ed, &em, &ey);
        //printf("\nTime Of Exit in HH.MM format :");
        //scanf("%d.%d", &eh,&eM);
        printf("\n\t==================================== \n");
        printf("\n\tCAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("\n\t==================================== \n");
        printf("\n\tCar Number : %s", car[n].Car_No);
        
        timeIn(car[n].d,size, car[n].e);
        printf("\n\tday = %s\n", car[n].d);
        printf("\n\tDate & Time Of Entry : %s\n", car[n].e);
        printf("\n\tThank You & Welcome to the JUNGLE\n\n");
        car[n].parked=1;
        n++;
        return 0;
        //}while(x<=100, x++);{
    //    }
        
    }
    int CekOut()
    {
        int i, j;
        char temp[20];
        int found=0;
        printf("Enter Your Car Number : ");
        //Car_no = getchar();
        scanf("%s",temp);
        for(i=0;i<n;i++)
        if(strcmp(car[i].Car_No,temp)==0)
        {
                 found=1;j=i;
        }
         
       if(found==1){
    
        printf("\n\t==================================== \n");
        printf("\n\tCAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("\n\t==================================== \n");
        printf("\n\tCar Number : %s", temp);
        printf("\n\tday = %s", car[j].d);
        printf("\n\tDate & Time Of Entry : %s", car[j].e);
        timeIn(car[i].d,size, car[i].e);
        printf("\n\tday = %s", car[i].d);
        printf("\n\tDate and Time Of Exit : %s", car[i].e);
        
        
        printf("\n\tThank You & Welcome to the JUNGLE\n\n");
        //CalculateDuration(sh, sMin, eh, eM); 
        //printf("Total Amount : RM%2f", price);
        //printf("Total Amount : RM%2f", duration);
       }
       else
       {
          printf("Sorry Palte No. Not Found..\n"); 
       }
       return 0;
    }
    int CalculateDuration(int HourIn, int MinIn, int HourOut, int MinOut)
    {
        int DurationTime, DH, DM;
        int TimeIn = (HourIn*60) + MinIn;
        int TimeOut = (HourOut*60) + MinOut;
    
        if (  HourIn>>HourOut )
        {
            DurationTime = (TimeOut+1440) - TimeIn;
            DH = DurationTime/60;
            DM = DurationTime%60;
        }
        else 
        {
            DurationTime = TimeOut - TimeIn;
            DH = DurationTime/60;
            DM = DurationTime%60;
        }
        printf("\n\tParking Time : %d.%d " , DH, DM);
        return 0;
    }
    
    int timeIn(char *day, int SIZE, char *t)
    {
        time_t nowtime;
        struct tm *ptr_time;
        day[SIZE];
        t[SIZE];
    
        time(&nowtime);    //Get the current time
    
        ptr_time = localtime(&nowtime);    //localtime returns a pointer to a tm structure
        strftime(day, SIZE, "%a ", ptr_time);    //Convert time to specific string
        strftime(t, SIZE, "%d/%m/%Y at %X", ptr_time);    //Convert time to specific string
        return 0;
    }
    int timeOut() {
    time_t nowtime;
    struct tm *ptr_time;
    char buffer[80];
    
    time(&nowtime);    //Get the current time
    
    ptr_time = localtime(&nowtime);    //localtime returns a pointer to a tm structure
    
    strftime(buffer, 80, "%a %d/%m/%Y at %X", ptr_time);    //Convert time to specific string
    printf("%s\n", buffer);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I don't understand your problem.

    In which part do you want to manually enter the time?
    Where and how do you want to store it?
    What do you want to do with that time?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by AndiPersti View Post
    I don't understand your problem.

    In which part do you want to manually enter the time?
    Where and how do you want to store it?
    What do you want to do with that time?

    Bye, Andreas
    I mean.. the date and day it will detect automatic from the system but the time we input manually.. but i already solved it.. but now i having problem how to arrange and calculate the fee for the parking.. because it sperated into weekday and weekend day... mon to friday different fee and saturday and sunday different fee.. how did the syntax looked like?
    my latest code.. but i havent put the selection for the payment of fee...
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<time.h>
    #include<stdlib.h>
    #include <string.h>
    #define size 80
    #define MAX 100    
    struct carPark{
        char Car_No[20];
        int parked;
        char d[size];
        char e[size];
        char buf[20];
        int sh; int sMin; int eh; int eM; int Duration; int Total;
    }car[100];
    
    int input();
    void display();
    int timeIn(char[],int,char[]);
    int timeOut();
    int dateIn(char[],int,char[]);
    int dateOut();
    int CekIn();
    int CekOut();
    int CalculateDuration(int , int, int, int  );
    int sd, sm, sy, ed, em, ey;
    int save[100][2];
    char a;
    char  days, cont; //Car_no[10],
    double price;
    int n=0;
    
    int main()
    {
        int option;
        static int c = 0;
        cont:
    
        for (;;)
        {
        printf("CAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("==================================== \n");
        printf("1 : In\n");
        printf("2 : Out\n");
        printf("0 : Exit\n");
        printf("==================================== \n");
        printf("Your Option : ");
        scanf("%d", &option);
                
            switch(option)
            {
            case 0:
                {
                    printf("Do you really want to exit ? \n(enter 'Y' for exit & enter 'N' for Continue) : ");
                    cont = getchar();
                    scanf("%c", &cont);
                    if ((cont == 'y') || (cont == 'Y'))
                    {
                        return 0;
                    }
                    else if((cont == 'n') || (cont == 'N'))
                    {
                        printf("\nThe system will Continue\n\n");
                        goto cont;
                    }
                }
            case 1:{
                
                if (c>=5)
                {
                    printf("Full Parking \n\n");
                }
                else
                {
                c++;
                printf("\ntotal car : %d\n\n", c);
                CekIn();
                
                }
                break;
            }
            case 2:{
                if (c<=0)
                    {
                    printf("No car parking here \n\n");
                    }
                else{
                    printf("total car : %d\n\n", c);
                    CekOut();
                    c--;
                }
                break;
            }
            default:
            {
                printf("wrong Selection ");
                break;
            }
        }
            system("PAUSE");
            printf("\n");
        }
    }
    int CekIn()
    {
        
        //do{
        printf("Enter Your Car Number : ");
        scanf("%s", car[n].Car_No);
        //printf("Days : ");
        //scanf("%s", &days);
        printf("\nTime Of Parking in HH.MM format :");
        scanf("%d.%d", &car[n].sh,&car[n].sMin);
    
        printf("\n\t==================================== \n");
        printf("\n\tCAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("\n\t==================================== \n");
        printf("\n\tCar Number : %s", car[n].Car_No);
        timeIn(car[n].d,size, car[n].e);
        printf("\n\tDate : %s",car[n].e);
        printf("\n\tDay = %s", car[n].d);
        printf("\n\tTime Of Entry : ");
        printf("%2d.%2d",car[n].sh,car[n].sMin);
        //timeIn();
        printf("\n\tThank You & Welcome to the JUNGLE\n\n");
        car[n].parked=1;
        n++;
        
        //}while(n<100);{
            //printf("Sorry, parking lot is full\n");
    //}
        return 0;
    }
    int CekOut()
    {
        char temp[20];
        int i, j;
        int found=0;
        printf("Enter Your Car Number : ");
        //Car_no = getchar();
        scanf("%s", temp);
        for(i=0;i<n;i++)
            if(strcmp(car[i].Car_No,temp)==0)
            {
                 found=1;
                 j=0;
            }
         
            if(found==1){
                printf("\nTime Of Exit in HH.MM format :");
                scanf("%d.%d", &car[i].eh,&car[i].eM);
                car[n].parked=0;
                printf("\n\t==================================== \n");
                printf("\n\tCAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
                printf("\n\t==================================== \n");
                printf("\n\tCar Number : %s", temp);
                printf("\n\tTime Of Entry : ");
                printf("%d.%d",car[j].sh,car[j].sMin);
                printf("\n\tTime Of Exit : ");
                printf("%d.%d",car[i].eh, car[i].eM);
                CalculateDuration(car[j].sh, car[j].sMin, car[i].eh, car[i].eM); 
                printf("\n\tTotal Amount : RM%2f", price);
                printf("\n\tThank You & Welcome to the JUNGLE\n\n");
                //i++;
               }
        
               else
               {
                  printf("Sorry Palte No. Not Found..\n"); 
               }
        
       return 0;
    }
    int CalculateDuration(int HourIn, int MinIn, int HourOut, int MinOut)
    {
        int DurationTime, DH, DM;
        int TimeIn = (HourIn*60) + MinIn;
        int TimeOut = (HourOut*60) + MinOut;
    
        if (  HourIn>>HourOut )
        {
            DurationTime = (TimeOut+1440) - TimeIn;
            DH = DurationTime/60;
            DM = DurationTime%60;
        }
        else 
        {
            DurationTime = TimeOut - TimeIn;
            DH = DurationTime/60;
            DM = DurationTime%60;
        }
        printf("\n\tParking Time : %d.%d " , DH, DM);
    
        return DH, DM;
    }
    int timeIn(char *day, int SIZE, char *t)
    {
        time_t nowtime;
        struct tm *ptr_time;
        day[SIZE];
        t[SIZE];
    
        time(&nowtime);    //Get the current time
    
        ptr_time = localtime(&nowtime);    //localtime returns a pointer to a tm structure
        strftime(day, SIZE, "%a ", ptr_time);    //Convert time to specific string
        strftime(t, SIZE, "%d/%m/%Y", ptr_time);    //Convert time to specific string
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How would you calculate the different fees on different days, without the computer?

    Do that nice and slow, a few times, and you'll start seeing the pattern of the logic you need to add to the program. An if/else statement is all you need.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    Quote Originally Posted by Adak View Post
    How would you calculate the different fees on different days, without the computer?

    Do that nice and slow, a few times, and you'll start seeing the pattern of the logic you need to add to the program. An if/else statement is all you need.
    i still cannt get it once it coded.. take a look the table.. once it state the time from 9am to 5pm and the rest time is free.. i already getting confuse..
    Manual time-c program-screenshot_1-png

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For dealing with days, since you have a tm already, maybe use this format, and get a day number.

    Code:
    %w Replaced by the weekday as a decimal number (0−6), where Sunday is 0. tm_wday
    Either use 0-6 for the days of the week, or 1-7. Check which days get which numbers, because they don't always use Sunday as the lowest numbered day of the week.

    Now the details need to be sorted out. What if a car parks on Friday at Noon, but stays until 2100 hrs 9pm. Charge the higher rate until 5 pm and free after that, right?

    Some variables would be handy: mfFirstHalf, mfSecHalf, mfAfter1Hr, mfAfter24Hr

    So a rate calculation includes calculating each of these, and then adding them up:

    if(day is Monday-Friday && timeIn < 1700 hours, they pay the top row schedule, until 1700 (1700 minus timeIn). Say they arrived at Noon
    Code:
      Covers the first hour,        leaving 4 hours * 2 to get the half hours
    ===========================================================
       mfFirstHalf + mfSecHalf + mfAfter1HrPerHalfHr 
    fee=   .20     +   .50     +   (8*1.6)
    And I'd make these non-free Rates into #defines and add them above main()
    Code:
    #define mfFirstHalf .20       //note: no semi-colon on these
    The best way to stay clear on these things is to use VERY descriptive variable names. You have a more realistic version of this problem, which makes it more complex, but also, more fun to figure out.
    Last edited by Adak; 12-13-2012 at 05:38 PM.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    ok this my latest code.. but i dont know how it gonna detect whether its saturday or sunday.. i only tried to code the payment.. now i still think about to diff the weekday and weekend fee
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<time.h>
    #include<stdlib.h>
    #include <string.h>
    #define size 80
    #define MAX 100    
    struct carPark{
        char Car_No[20];
        int parked;
        char d[size];
        char e[size];
        char buf[20];
        int sh; int sMin; int eh; int eM; int Duration; int Total;
        int sd,sm,ed,em;
    }car[100];
    
    int input();
    void display();
    int timeIn(char[],int,char[]);
    int timeOut();
    int dateIn(char[],int,char[]);
    int dateOut();
    float calcAmount(int);
    int CekIn();
    int CekOut();
    int CalculateDuration(int , int, int, int,int,int,int,int  );
    int sd, sm, sy, ed, em, ey;
    int save[100][2];
    char a;
    char  days, cont; //Car_no[10],
    double price;
    int n=0;
    
    int main()
    {
        int option;
        static int c = 0;
        cont:
    
        for (;;)
        {
        printf("CAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("==================================== \n");
        printf("1 : In\n");
        printf("2 : Out\n");
        printf("0 : Exit\n");
        printf("==================================== \n");
        printf("Your Option : ");
        scanf("%d", &option);
                
            switch(option)
            {
            case 0:
                {
                    printf("Do you really want to exit ? \n(enter 'Y' for exit & enter 'N' for Continue) : ");
                    cont = getchar();
                    scanf("%c", &cont);
                    if ((cont == 'y') || (cont == 'Y'))
                    {
                        return 0;
                    }
                    else if((cont == 'n') || (cont == 'N'))
                    {
                        printf("\nThe system will Continue\n\n");
                        goto cont;
                    }
                }
            case 1:{
                
                if (c>=5)
                {
                    printf("Full Parking \n\n");
                }
                else
                {
                c++;
                printf("\ntotal car : %d\n\n", c);
                CekIn();
                
                }
                break;
            }
            case 2:{
                if (c<=0)
                    {
                    printf("No car parking here \n\n");
                    }
                else{
                    printf("total car : %d\n\n", c);
                    CekOut();
                    c--;
                }
                break;
            }
            default:
            {
                printf("wrong Selection ");
                break;
            }
        }
            system("PAUSE");
            printf("\n");
        }
    }
    int CekIn()
    {
        
        //do{
        printf("Enter Your Car Number : ");
        scanf("%s", car[n].Car_No);
        //printf("Days : ");
        //scanf("%s", &days);
        printf("\nTime Of Parking in HH.MM format :");
        scanf("%d.%d", &car[n].sh,&car[n].sMin);
    
        printf("\n\t==================================== \n");
        printf("\n\tCAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
        printf("\n\t==================================== \n");
        printf("\n\tCar Number : %s", car[n].Car_No);
        timeIn(car[n].d,size, car[n].e);
        printf("\n\tDate : %s",car[n].e);
        printf("\n\tDay = %s", car[n].d);
        printf("\n\tTime Of Entry : ");
        printf("%2d.%2d",car[n].sh,car[n].sMin);
        //timeIn();
        printf("\n\tThank You & Welcome to the JUNGLE\n\n");
        car[n].parked=1;
        n++;
        
        //}while(n<100);{
            //printf("Sorry, parking lot is full\n");
    //}
        return 0;
    }
    int CekOut()
    {
        char temp[20];
        int i, j;
        int found=0;
        printf("Enter Your Car Number : ");
        //Car_no = getchar();
        scanf("%s", temp);
        for(i=0;i<n;i++)
            if(strcmp(car[i].Car_No,temp)==0)
            {
                 found=1;
                 j=0;
            }
         
            if(found==1){
                printf("\nTime Of Exit in HH.MM format :");
                scanf("%d.%d", &car[i].eh,&car[i].eM);
                car[n].parked=0;
                printf("\n\t==================================== \n");
                printf("\n\tCAR PARKING MANAGEMENT SYSTEM (CPMS) \n");
                printf("\n\t==================================== \n");
                printf("\n\tCar Number : %s", temp);
                printf("\n\tDay = %s", car[j].d);
                printf("\n\tDate Of Entry : %s", car[j].e);
                timeIn(car[i].d,size, car[i].e);
                printf("\n\tDate Of Exit : %s", car[i].e);
                printf("\n\tTime Of Entry : ");
                printf("%d.%d",car[j].sh,car[j].sMin);
                printf("\n\tTime Of Exit : ");
                printf("%d.%d",car[i].eh, car[i].eM);
                CalculateDuration(car[j].sh, car[j].sMin, car[i].eh, car[i].eM,car[j].sd, car[j].sm, car[i].ed, car[i].em);
                printf("\n\tThank You & Welcome to the JUNGLE\n\n");
                //i++;
               }
        
               else
               {
                  printf("Sorry Palte No. Not Found..\n"); 
               }
        
       return 0;
    }
    int CalculateDuration(int HourIn, int MinIn, int HourOut, int MinOut,int dateIn,int monthIn, int dateOut, int monthOut)
    {
        int DurationTime, DH, DM;
        int TimeIn = (HourIn*60) + MinIn;
        int TimeOut = (HourOut*60) + MinOut;
        if(dateIn == dateOut && monthIn == monthOut)
        {
            if (  HourIn>>HourOut || (HourIn==HourOut && MinIn >= MinOut))
            {
                DurationTime = (TimeOut+1440) - TimeIn;
                DH = DurationTime/60;
                DM = DurationTime%60;
    
            }
            else 
            {
                DurationTime = TimeOut - TimeIn;
                DH = DurationTime/60;
                DM = DurationTime%60;
            }
        }
        else if(dateOut>dateIn && monthIn == monthOut)
        {
            int diff = dateOut - dateIn;
            if (  HourIn>>HourOut || (HourIn==HourOut && MinIn >= MinOut))
            {
                DurationTime = (TimeOut+1440) - TimeIn;
                DH = DurationTime/60;
                DM = DurationTime%60;
    
            }
            else 
            {
                DurationTime = (TimeOut - TimeIn) +(diff*1440);
                DH = DurationTime/60;
                DM = DurationTime%60;
            }
        }
            printf("\n\tParking Time : %d hour %d minutes" , DH, DM);
    
        calcAmount(DurationTime);
        return DH,DM;
    }
    int timeIn(char *day, int SIZE, char *t)
    {
        time_t nowtime;
        struct tm *ptr_time;
        day[SIZE];
        t[SIZE];
    
        time(&nowtime);    //Get the current time
    
        ptr_time = localtime(&nowtime);    //localtime returns a pointer to a tm structure
        strftime(day, SIZE, "%a ", ptr_time);    //Convert time to specific string
        strftime(t, SIZE, "%d/%m/%Y", ptr_time);    //Convert time to specific string
        return 0;
    }
    int timeOut() 
    {
    time_t nowtime;
    struct tm *ptr_time;
    char buffer[80];
    
    time(&nowtime);    //Get the current time
    
    ptr_time = localtime(&nowtime);    //localtime returns a pointer to a tm structure
    
    strftime(buffer, 80, "%a %d/%m/%Y", ptr_time);    //Convert time to specific string
    printf("%s\n", buffer);
    
    return 0;
    }
    float calcAmount(int duration)
    {
        double amount;
        if (duration<=30)
        {
            amount = 0.20;
        }
        else if(duration<=60)
        {
            amount = 0.20 + 0.50;
        }
        else if(duration<1440)
        {
            double a = 1.6 * (duration/60);
            amount = 0.20 + 0.50 + a;
        }
        else if(duration >=1440)
        {
            amount = 50.00*(duration/1440);
        }
        printf("\n\tTotal Amount : RM%.2f" , amount);
        return amount;
    }
    Last edited by aquilina; 12-13-2012 at 06:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. raw time program time incorrect
    By ratmo in forum C++ Programming
    Replies: 0
    Last Post: 06-19-2012, 05:16 AM
  2. C# manual updates
    By Shingetsu Kurai in forum C# Programming
    Replies: 4
    Last Post: 09-16-2011, 07:48 AM
  3. time program showing time since epoch?
    By cus in forum Linux Programming
    Replies: 5
    Last Post: 01-10-2009, 01:56 PM
  4. C/C++ Manual
    By JonnyHatesJesus in forum C Programming
    Replies: 7
    Last Post: 07-17-2002, 06:12 AM
  5. c builder manual
    By h_kharkan in forum Windows Programming
    Replies: 0
    Last Post: 12-14-2001, 02:02 AM