samedi 28 février 2015

Error with program to continue after for it reaches the for loop



When i have reached my for loop, my program crashes and I really dont know what else to do. Im making an attendance record system that will prompt the user to enter there ID. Program will search a file containing their information and search for the ID entered. After it has found it, the program should ask a series of questions. After all have completed, the loop should begin again for another student. Below is a snippet of the main code.



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

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

int main (void) {
char ffname[28], flname[28], ans;
int fID, i, id_;
information array[3];
FILE *kfile;

i = 0;

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

//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("C:\\Users\\keneil\\Desktop\\information.txt","r")) == NULL) {
perror("Error while opening file");
} else {
//while the file is opened and not at the end, the strings are stored into variables which forms an array of strings
while (!feof(kfile)) {
fscanf(kfile, "%s %s %d", ffname, flname, &fID);
array[i].fname = strdup(ffname);
array[i].lname = strdup(flname);
array[i].code = fID;
i++;
}
fclose(kfile);
}

Next:
printf("Please enter your ID: ");
scanf("%d", &id_);

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

//printf("\n");

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

switch (ans){
case 'y':
if((int)buf < ((int)"08:00" )){
printf("You are early for school.");
}
else{
printf("You are late for school.");
system("cls");
}
break;
case 'n':
system ("cls");
goto Next;
break;
default:
printf("invalid answer");
}
}



getch();
return 0;
}



Aucun commentaire:

Enregistrer un commentaire