Member Avatar for ragnacrap

Does anybody know how to make a program that calculates your work hours? I tried this code, but it's going all wrong.

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


char* timein[5], timeout[5], p;
int totaltime, ticktock, hour, minute, rate;

FILE* f;


int main(){
    clrscr();
    CreateTimeFile();
    GetTimes();
    DisplayFinalInfo();
    getch();
}

CreateTimeFile(){
    f = fopen("time.txt","w");
    fclose(f);
}

GetTimes(){
    f = fopen("time.txt","a");
    printf("\nEnter time in 24-hour format. \nWARNING: An error at any point in the input will result in needing to repeat the process.\nEnter time-in for Monday: ");
    scanf("%c",&timein);
    ValidateTimeIn();
    printf("\nEnter time-out for Monday: ");
    scanf("%c",&timeout);
    ValidateTimeOut();
    AppendTimes();

    printf("\nEnter time-in for Tuesday: ");
    scanf("%c",&timein);
    ValidateTimeIn();
    printf("\nEnter time-out for Tuesday: ");
    scanf("%c",&timeout);
    ValidateTimeOut();
    AppendTimes();

    printf("\nEnter time-in for Wednesday: ");
    scanf("%c",&timein);
    ValidateTimeIn();
    printf("\nEnter time-out for Wednesday: ");
    scanf("%c",&timeout);
    ValidateTimeOut();
    AppendTimes();

    printf("\nEnter time-in for Thursday: ");
    scanf("%c",&timein);
    ValidateTimeIn();
    printf("\nEnter time-out for Thursday: ");
    scanf("%c",&timeout);
    ValidateTimeOut();
    AppendTimes();

    printf("\nEnter time-in for Friday: ");
    scanf("%c",&timein);
    ValidateTimeIn();
    printf("\nEnter time-out for Friday: ");
    scanf("%c",&timeout);
    ValidateTimeOut();
    AppendTimes();

    fclose(f);
/*  ComputeSalary();    */
/*  DisplayTime();      */
}

ValidateTimeIn(){
    if (timein[0] > 2 || timein[3] > 5){
        printf("Input Error! Restarting input process.\n");
        GetTimes();
    }
    if (timein[0] = 2 && timein[1] > 3){
        printf("Input Error! Restarting input process.\n");
        GetTimes();
        }
}

ValidateTimeOut(){
    if (timeout[0] > 2 || timeout[3] > 5){
        printf("Input Error! Restarting input process.\n");
        GetTimes();
    }
    if (timeout[0] = 2 && timeout[1] > 3){
        printf("Input Error! Restarting input process.\n");
        GetTimes();
        }
}

AppendTimes(){
    fprintf(f,"%s %s|",timein, timeout);
    AddToTotalTime();
}

AddToTotalTime(){
    /*-convert timein and timeout to time-*/
    p = timeout;
    atoi(p) = hour;
    while (*p){
        if (*p++ == ':')
            atoi(p) = minute;
    }
    timeout = (hour * 60) + minute;

    p = timein;
    atoi(p) = hour;
    while (*p){
        if (*p++ == ':')
            atoi(p) = minute;
    }
    timein = (hour * 60) + minute;

    ticktock = timeout - timein;
    totaltime += ticktock;
}

DisplayFinalInfo(){

    hour = totaltime / 60;
    minute = totaltime % 60;
    printf("Total Work Time: %i Hours and %i Minutes\n", hour, minute);
}

I know I'm doing plenty of mistakes here. I just don't know what exactly. And no, I'm not supposed to work it in a different program like C#.

Recommended Answers

All 9 Replies

I do not believe this

char* timein[5], timeout[5], p;

is doing what you think.

It creates

timein as an array of 5 pointers to char (char *)
timeout as an array of 5 char
p as a char.

You should try

char timein[5], timeout[5], *p;

or better still only declare 1 variable per statement then there is no room for confusion

char timein[5];
char timeout[5];
char *p;

Also ValidateTimeIn and ValidateTimeOut have exactly the same structure, if you used an input parameter it could be 1 function.

p is used locally to ValidateTimeIn, ValidateTimeOut and AddToTotalTime and should be declared locally to those functions not globally.

The first probelm is

scanf("%c",&timein);

timein is an array of char pointers , so the correct way of reading if you want to store the time in string format

scanf("%s",timein[index]);

but first you need to allocate memory for timein[5].

timein[index] = (char *)malloc(sizeof(char)*NO_OF_CHARS);

you need to either use fixed number for NO_OF_CHARS or calculate using

 strlen();

why are you using timein[5], do you want to store the time in specific format , i mean 14:30 or 09:20 etc..

The second probelm is, your

ValidateTimeIn()
ValidateTimeOut()

Dont understand what do you want to achieve here

if (timein[0] > 2 || timein[3] > 5){
    ......;

if (timein[0] = 2 && timein[1] > 3){
    ......;

in the second if use '==' instead of '='

Member Avatar for ragnacrap

Thanks. I was doing the pointers wrong the entire time.

scanf("%s",timein[index]);

Is the index needed as is?

As for the ValidateTimeIn and ValidateTimeOut, the first if condition is supposed to make an error message if the tens in the hour number is more than 2 or the tens in the minute number is more than 5. Then, the next if condition will check if the tens in the hour is 2 AND if the ones in the hour is more than three or not.

I need the hour and minute converted to integers as well for another part of the program.

Member Avatar for ragnacrap

Hello?

The index may r maynot be needed depending on what type you actually intend timein to have.

There are functions in the C library that will help you convert string to integer, strtol being the one I would use.

Member Avatar for ragnacrap

From what little I can do with the strtol, I can extract the hour. and the minute.

But nobody's telling me how to solve the ValidateTimeIn and -Out.

Once you have extracted the hours and minutes then check that those numbers are sensible for what they represent.

Member Avatar for ragnacrap
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<time.h>

char timein[5], timeout[5];
int hour, minute, intime, outtime, ticktock;
int totaltime = 0;
char *token;
const char d[2] = ":";

FILE* f;

int main(){
    clrscr();
    CreateTimeFile();
    GetTimes();
    getch();
}

CreateTimeFile(){
    f = fopen("time.txt","w");
    fclose(f);
}

GetTimes(){
    f = fopen("time.txt","a");
    printf("\nEnter time in 24-hour format. \nWARNING: An error at any point in the input will result in needing to repeat the process.\nEnter time-in for Monday: ");
    scanf("%s",&timein);
    ExtractTimeIn();
    printf("\nEnter time-out for Monday: ");
    scanf("%s",&timeout);
    ExtractTimeOut();
    AppendTimes();

    fclose(f);
}

ExtractTimeIn(){
    /* get the first token */
    token = strtok(timein, s);
    hour = atoi(token);

    /* walk through other token */
    while( token != NULL )
    {
        printf( " %s\n", token);
        token = strtok(NULL, s);
    }
    minute = atoi(token);
    ValidateTimeIn();
}

ValidateTimeIn(){
    printf("Hour: %d\n", hour);
    printf("Minute: %d\n", minute);
}

ExtractTimeOut(){
    /* get the first token */
    token = strtok(timeout, s);
    hour = atoi(token);

    /* walk through other token */
    while( token != NULL )
    {
        printf( " %s\n", token);
        token = strtok(NULL, s);
    }
    minute = atoi(token);
    ValidateTimeOut();
}

ValidateTimeOut(){
    printf("Hour: %d\n", hour);
    printf("Minute: %d\n", minute);
}

AppendTimes(){
    fprintf(f,"%s %s|",timein, timeout);
    AddToTotalTime();
}

AddToTotalTime(){
    ticktock = outtime - intime;
    totaltime = totaltime + ticktock;
}

What am I doing wrong this time?

That is rather an opened question but here goes

Line 8 - 12 much more global data than is required, several variables declared global that should be declared local to functions or as function parameters (token, hour, minute, d and f being good examples); d is not used anywhere.

Line 22, 28, 41, 56, 61, 76, 81, 86 All functions declare without a return type, no functions have return statements, no functions use any input parameters so they can't be reused, for example ExtractTimeIn and ExtractTimeOut with no parameters could be a single function ExtractTime taking a const char * (or char *) parameter pointing to the string to extract the time from.

Line 31 and 34 you erroneously have a & infront of the char array variable.

This results is this output from my compiler

test.c: In function 'main':
test.c:17:5: warning: implicit declaration of function 'clrscr'
test.c:18:5: warning: implicit declaration of function 'CreateTimeFile'
test.c:19:5: warning: implicit declaration of function 'GetTimes'
test.c: At top level:
test.c:23:1: warning: return type defaults to 'int'
test.c:28:1: warning: return type defaults to 'int'
test.c: In function 'GetTimes':
test.c:31:5: warning: format '%s' expects type 'char ', but argument 2 has type 'char ()[5]'
test.c:32:5: warning: implicit declaration of function 'ExtractTimeIn'
test.c:34:5: warning: format '%s' expects type 'char ', but argument 2 has type 'char ()[5]'
test.c:35:5: warning: implicit declaration of function 'ExtractTimeOut'
test.c:36:5: warning: implicit declaration of function 'AppendTimes'
test.c: At top level:
test.c:41:1: warning: return type defaults to 'int'
test.c: In function 'ExtractTimeIn':
test.c:43:28: error: 's' undeclared (first use in this function)
test.c:43:28: note: each undeclared identifier is reported only once for each function it appears in
test.c:53:5: warning: implicit declaration of function 'ValidateTimeIn'
test.c: At top level:
test.c:56:1: warning: return type defaults to 'int'
test.c:61:1: warning: return type defaults to 'int'
test.c: In function 'ExtractTimeOut':
test.c:63:29: error: 's' undeclared (first use in this function)
test.c:73:5: warning: implicit declaration of function 'ValidateTimeOut'
test.c: At top level:
test.c:76:1: warning: return type defaults to 'int'
test.c:81:1: warning: return type defaults to 'int'
test.c: In function 'AppendTimes':
test.c:83:5: warning: implicit declaration of function 'AddToTotalTime'
test.c: At top level:
test.c:86:1: warning: return type defaults to 'int'
test.c: In function 'AddToTotalTime':
test.c:89:1: warning: control reaches end of non-void function
test.c: In function 'AppendTimes':
test.c:84:1: warning: control reaches end of non-void function
test.c: In function 'ValidateTimeOut':
test.c:79:1: warning: control reaches end of non-void function
test.c: In function 'ExtractTimeOut':
test.c:74:1: warning: control reaches end of non-void function
test.c: In function 'ValidateTimeIn':
test.c:59:1: warning: control reaches end of non-void function
test.c: In function 'ExtractTimeIn':
test.c:54:1: warning: control reaches end of non-void function
test.c: In function 'GetTimes':
test.c:39:1: warning: control reaches end of non-void function
test.c: In function 'CreateTimeFile':
test.c:26:1: warning: control reaches end of non-void function
test.c: In function 'main':
test.c:21:1: warning: control reaches end of non-void function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.