C
chutsu
Ok Here is a problem, I got a imaginary database program that I need
to code, to add a patient I have function inser_patient. but when I
try to input the details it doesn't quite work the way I wanted it to.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAXPATIENTS 20
struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};
struct details patient[MAXPATIENTS];
int npatients = 0;
int insert_patient(int index, struct details newpatient);
int main (void) {
int Choice;
int id;
int loop = 1;
int n=0;
int error=0;
int limit;
int Qnum;
FILE * fptr; //Declares file pointer as "fptr"
fptr = fopen("queue.dat", "r"); //Opens queue.dat to fptr
//Reading
while (fscanf(fptr,"%d %s %c %s %d %d[^\n]", &patient[n].id,
patient[n].forename, &patient[n].initial, patient[n].surname,
&patient[n].day_of_entry, &patient[n].max_wait) != EOF){
n++;
}
fclose(fptr);
//Loop that continues the program
while(loop != 0){
//To keep program running
//Displaying Menu
printf("\n\n");
printf("%d \n",day_now());
printf("_________________________________________\n");
printf("|-----------NHS Queue Control-----------|\n");
printf("| Create New Patient \t- Press 1\t|\n");
printf("| Delete Patient \t- Press 2\t|\n");
printf("| Find Patient \t\t- Press 3\t|\n");
printf("| List Queue \t\t- Press 4\t|\n");
printf("| Treate Next Patient \t- Press 5\t| \n");
printf("| Quit \t\t\t- Press 6\t|\n");
printf("|_______________________________________|\n");
printf("Choice: ");
scanf("%d",&Choice);
printf("\n");
//Choice made, carrying out function
switch (Choice){
//New Patient
case 1:
limit = n;
if(limit < 21){
list_queue();
printf("\nPlease enter the following details\n");
printf("What Queue Number would you like to place this patient
to?\n");
scanf("%d", &Qnum);
printf("Patient ID: ");
scanf("%d", &patient[20].id);
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);
printf("Surname: ");
scanf("%s ", patient[20].surname);
printf("Maximum Waiting Time: ");
scanf("%d ", &patient[20].max_wait);
patient[20].day_of_entry = day_now();
error = insert_patient(Qnum, patient[20]);
if(error == -1){
printf("Error - Patient exists!\n");
}
else {
printf("\nPatient Inserted!\n");
}
}
else {
printf("Array is Full!\n");
}
break;
}
return 0;
}
//This is the add function
int insert_patient(int index, struct details newpatient) {
int i = 0;
int n = 0;
int y = find_patient_id(index) ;
int x;
int error;
FILE * fptr; //Declares file pointer as "fptr"
fptr = fopen("queue.dat", "r");
while (fscanf(fptr,"%d %s %c %s %d %d[^\n]", &patient[n].id,
patient[n].forename, &patient[n].initial, patient[n].surname,
&patient[n].day_of_entry, &patient[n].max_wait) != EOF){
n++;
}
fclose(fptr);
if(y == -1){
fptr = fopen("queue.dat", "w");
for(i=0;i<=index-2;i++){
fprintf(fptr,"%d ",patient.id);
fprintf(fptr,"%s ",patient.forename);
fprintf(fptr,"%c ",patient.initial);
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);
}
fprintf(fptr,"%d ",patient[20].id);
fprintf(fptr,"%s ",patient[20].forename);
fprintf(fptr,"%c ",patient[20].initial);
fprintf(fptr,"%s ",patient[20].surname);
fprintf(fptr,"%d ",patient[20].day_of_entry);
fprintf(fptr,"%d \n",patient[20].max_wait);
for(x=index;x<=n+1;x++){
fprintf(fptr,"%d ",patient[x].id);
fprintf(fptr,"%s ",patient[x].forename);
fprintf(fptr,"%c ",patient[x].initial);
fprintf(fptr,"%s ",patient[x].surname);
fprintf(fptr,"%d ",patient[x].day_of_entry);
fprintf(fptr,"%d \n",patient[x].max_wait);
}
error = 0;
}
else if (y != 0){
error = -1;
}
fclose(fptr);
return error;
}
to code, to add a patient I have function inser_patient. but when I
try to input the details it doesn't quite work the way I wanted it to.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAXPATIENTS 20
struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};
struct details patient[MAXPATIENTS];
int npatients = 0;
int insert_patient(int index, struct details newpatient);
int main (void) {
int Choice;
int id;
int loop = 1;
int n=0;
int error=0;
int limit;
int Qnum;
FILE * fptr; //Declares file pointer as "fptr"
fptr = fopen("queue.dat", "r"); //Opens queue.dat to fptr
//Reading
while (fscanf(fptr,"%d %s %c %s %d %d[^\n]", &patient[n].id,
patient[n].forename, &patient[n].initial, patient[n].surname,
&patient[n].day_of_entry, &patient[n].max_wait) != EOF){
n++;
}
fclose(fptr);
//Loop that continues the program
while(loop != 0){
//To keep program running
//Displaying Menu
printf("\n\n");
printf("%d \n",day_now());
printf("_________________________________________\n");
printf("|-----------NHS Queue Control-----------|\n");
printf("| Create New Patient \t- Press 1\t|\n");
printf("| Delete Patient \t- Press 2\t|\n");
printf("| Find Patient \t\t- Press 3\t|\n");
printf("| List Queue \t\t- Press 4\t|\n");
printf("| Treate Next Patient \t- Press 5\t| \n");
printf("| Quit \t\t\t- Press 6\t|\n");
printf("|_______________________________________|\n");
printf("Choice: ");
scanf("%d",&Choice);
printf("\n");
//Choice made, carrying out function
switch (Choice){
//New Patient
case 1:
limit = n;
if(limit < 21){
list_queue();
printf("\nPlease enter the following details\n");
printf("What Queue Number would you like to place this patient
to?\n");
scanf("%d", &Qnum);
printf("Patient ID: ");
scanf("%d", &patient[20].id);
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);
printf("Surname: ");
scanf("%s ", patient[20].surname);
printf("Maximum Waiting Time: ");
scanf("%d ", &patient[20].max_wait);
patient[20].day_of_entry = day_now();
error = insert_patient(Qnum, patient[20]);
if(error == -1){
printf("Error - Patient exists!\n");
}
else {
printf("\nPatient Inserted!\n");
}
}
else {
printf("Array is Full!\n");
}
break;
}
return 0;
}
//This is the add function
int insert_patient(int index, struct details newpatient) {
int i = 0;
int n = 0;
int y = find_patient_id(index) ;
int x;
int error;
FILE * fptr; //Declares file pointer as "fptr"
fptr = fopen("queue.dat", "r");
while (fscanf(fptr,"%d %s %c %s %d %d[^\n]", &patient[n].id,
patient[n].forename, &patient[n].initial, patient[n].surname,
&patient[n].day_of_entry, &patient[n].max_wait) != EOF){
n++;
}
fclose(fptr);
if(y == -1){
fptr = fopen("queue.dat", "w");
for(i=0;i<=index-2;i++){
fprintf(fptr,"%d ",patient.id);
fprintf(fptr,"%s ",patient.forename);
fprintf(fptr,"%c ",patient.initial);
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);
}
fprintf(fptr,"%d ",patient[20].id);
fprintf(fptr,"%s ",patient[20].forename);
fprintf(fptr,"%c ",patient[20].initial);
fprintf(fptr,"%s ",patient[20].surname);
fprintf(fptr,"%d ",patient[20].day_of_entry);
fprintf(fptr,"%d \n",patient[20].max_wait);
for(x=index;x<=n+1;x++){
fprintf(fptr,"%d ",patient[x].id);
fprintf(fptr,"%s ",patient[x].forename);
fprintf(fptr,"%c ",patient[x].initial);
fprintf(fptr,"%s ",patient[x].surname);
fprintf(fptr,"%d ",patient[x].day_of_entry);
fprintf(fptr,"%d \n",patient[x].max_wait);
}
error = 0;
}
else if (y != 0){
error = -1;
}
fclose(fptr);
return error;
}