mardi 3 mars 2015

If ID code hasn't been entered - Attendance Sytem

What I want to do is, if someone hasn't entered their ID code, it will print absent in a file. I'm not sure where to implement this in my code though. The full code is below, also if any corrections needed, feel free to critique.



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


typedef struct record {
char *fname;
char *lname;
int code;
} information;


int main (void) {
char ffname[28], flname[28], ans, ans1, status[15];
int fID, i, j, id_, x, count;
FILE *kfile, *ufile;
x = 0;
j = 0;
i = 0;
count = 0;

char buf[150];
time_t curtime;
struct tm* loc_time;
information array[100];

printf(" **********Attendance Recording System**********\n");
printf(" MENU \n");
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
strftime (buf,150, "%I:%M %p.\n", loc_time);


//prints error message if file cannot be found within the system

if ((kfile = fopen("information.txt", "r")) == NULL) //If the file path is incorrect, an error message is displayed
{
fprintf(stderr, "Error while opening file (%d: %s)\n",errno, strerror(errno)); //Error message that will be displayed if file path is incorrect

return;
}

//while the file is opened and not at the end, the strings are stored into variables which forms an array of strings
for (x = 0; x < 200; x++) {
if (fscanf(kfile, "%s %s %d", ffname, flname, &fID) != 3) //Reads the contents of the file
break;
array[x].fname = strdup(ffname);
array[x].lname = strdup(flname);
array[x].code = fID;
}
fclose(kfile);

ufile= fopen("C:\\Users\\Student\\Desktop\\update.txt","w");
fprintf(ufile,"First Name Last Name ID Status Time Arrived");

while (j < x){
Next:
printf("Please enter your ID: ");
scanf("%d", &id_);

for (i = 0; i < x; i++) {



if(array[i].code == id_){

printf("Are you %s %s?\n", array[i].fname, array[i].lname);
printf("[y/n only]: ");
scanf(" %c", &ans);

switch (ans){
case 'y':
strcpy(status, "Present");
if((int)buf < ((int)"08:00" )){
printf("Early.\n");

}
else{
printf("You are late for school.\n");
}


printf("Continue?\n[y/n only]:");
scanf(" %c", &ans1);

fprintf(ufile,"\n");
printf("You are early for school.\n");
fprintf(ufile,"%-15s%-14s%2d%12s ",array[i].fname, array[i].lname, array[i].code, status);
fprintf(ufile,"%16s",(buf));


if(ans1 == 'y'){
break;
case 'n':
goto Next;
break;
default:
printf("invalid answer");
}
if (ans1 == 'n'){
exit(EXIT_SUCCESS);
}
}



}
}

j++;

}
fclose(ufile);

getch();
return 0;
}

Aucun commentaire:

Enregistrer un commentaire