What is wrong? I can't add...

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;
}
 
B

Barry Schwarz

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.

Are we supposed to guess what you are talking about? What did you
want? What actually happened?
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

You should verify that fopen succeeded.
//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){

This is the wrong test. fscanf returns EOF only if no data was
converted. You should be proceeding only if the return value is 6.

You never check for n overflowing the number of elements in your
array.
}
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){

You never use limit so why do you care what it's value is.
list_queue();

There is no prototype in scope for this function.
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: ");

On a buffered system, calls to printf that don't end with a '\n' may
not appear before the system waits for input. If you want the input
to appear on the same line as the prompt, you should add
fflush(stdout);
to insure the buffer is flushed to the stream.
scanf("%d", &patient[20].id);

Every use of patient[20] invokes undefined behavior. The valid
subscripts are 0 through 19 (MAXPATIENTS-1).
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.
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]);

While it is perfectly legal to pass a struct, the common
recommendation is to pass a pointer to the struct if the structure is
larger that trivial.
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) ;

There is no prototype in scope for this function. You also forgot to
provide the definition of this function.
int x;
int error;

FILE * fptr; //Declares file pointer as "fptr"

Do you really think this comment contains any useful information?
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){

You already read the records into patient back in main. Why are you
reading them again?
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);


You write the fields of the record with no intervening characters
between them. How is your call to fscanf supposed to know where
forename ends and initial begins?
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);


Ditto between surname and both integer fields or between day_of_entry
and max_wait.
}

fprintf(fptr,"%d ",patient[20].id);

patient[20] still does not exist.
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++){

Why n+1? This insures you will process extraneous data. Fortunately
(or un-), the data is initialized since patient is at file scope.

Did you mean n-1? That would make sense but the common idiom is
i < n, not i <= n-1.
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;
}


Remove del for email
 
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.

Are we supposed to guess what you are talking about? What did you
want? What actually happened?




#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

You should verify that fopen succeeded.
//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){

This is the wrong test. fscanf returns EOF only if no data was
converted. You should be proceeding only if the return value is 6.

You never check for n overflowing the number of elements in your
array.


}
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){

You never use limit so why do you care what it's value is.
list_queue();

There is no prototype in scope for this function.
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: ");

On a buffered system, calls to printf that don't end with a '\n' may
not appear before the system waits for input. If you want the input
to appear on the same line as the prompt, you should add
fflush(stdout);
to insure the buffer is flushed to the stream.
scanf("%d", &patient[20].id);

Every use of patient[20] invokes undefined behavior. The valid
subscripts are 0 through 19 (MAXPATIENTS-1).
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.
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]);

While it is perfectly legal to pass a struct, the common
recommendation is to pass a pointer to the struct if the structure is
larger that trivial.


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) ;

There is no prototype in scope for this function. You also forgot to
provide the definition of this function.
int x;
int error;
FILE * fptr; //Declares file pointer as "fptr"

Do you really think this comment contains any useful information?
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){

You already read the records into patient back in main. Why are you
reading them again?
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);


You write the fields of the record with no intervening characters
between them. How is your call to fscanf supposed to know where
forename ends and initial begins?
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);


Ditto between surname and both integer fields or between day_of_entry
and max_wait.
fprintf(fptr,"%d ",patient[20].id);

patient[20] still does not exist.
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++){

Why n+1? This insures you will process extraneous data. Fortunately
(or un-), the data is initialized since patient is at file scope.

Did you mean n-1? That would make sense but the common idiom is
i < n, not i <= n-1.
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;
}

Remove del for email



So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris
 
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.

Are we supposed to guess what you are talking about? What did you
want? What actually happened?




#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

You should verify that fopen succeeded.
//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){

This is the wrong test. fscanf returns EOF only if no data was
converted. You should be proceeding only if the return value is 6.

You never check for n overflowing the number of elements in your
array.


}
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){

You never use limit so why do you care what it's value is.
list_queue();

There is no prototype in scope for this function.
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: ");

On a buffered system, calls to printf that don't end with a '\n' may
not appear before the system waits for input. If you want the input
to appear on the same line as the prompt, you should add
fflush(stdout);
to insure the buffer is flushed to the stream.
scanf("%d", &patient[20].id);

Every use of patient[20] invokes undefined behavior. The valid
subscripts are 0 through 19 (MAXPATIENTS-1).
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.
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]);

While it is perfectly legal to pass a struct, the common
recommendation is to pass a pointer to the struct if the structure is
larger that trivial.


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) ;

There is no prototype in scope for this function. You also forgot to
provide the definition of this function.
int x;
int error;
FILE * fptr; //Declares file pointer as "fptr"

Do you really think this comment contains any useful information?
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){

You already read the records into patient back in main. Why are you
reading them again?
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);


You write the fields of the record with no intervening characters
between them. How is your call to fscanf supposed to know where
forename ends and initial begins?
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);


Ditto between surname and both integer fields or between day_of_entry
and max_wait.
fprintf(fptr,"%d ",patient[20].id);

patient[20] still does not exist.
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++){

Why n+1? This insures you will process extraneous data. Fortunately
(or un-), the data is initialized since patient is at file scope.

Did you mean n-1? That would make sense but the common idiom is
i < n, not i <= n-1.
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;
}

Remove del for email



So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris
 
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.

Are we supposed to guess what you are talking about? What did you
want? What actually happened?




#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

You should verify that fopen succeeded.
//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){

This is the wrong test. fscanf returns EOF only if no data was
converted. You should be proceeding only if the return value is 6.

You never check for n overflowing the number of elements in your
array.


}
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){

You never use limit so why do you care what it's value is.
list_queue();

There is no prototype in scope for this function.
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: ");

On a buffered system, calls to printf that don't end with a '\n' may
not appear before the system waits for input. If you want the input
to appear on the same line as the prompt, you should add
fflush(stdout);
to insure the buffer is flushed to the stream.
scanf("%d", &patient[20].id);

Every use of patient[20] invokes undefined behavior. The valid
subscripts are 0 through 19 (MAXPATIENTS-1).
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.
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]);

While it is perfectly legal to pass a struct, the common
recommendation is to pass a pointer to the struct if the structure is
larger that trivial.


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) ;

There is no prototype in scope for this function. You also forgot to
provide the definition of this function.
int x;
int error;
FILE * fptr; //Declares file pointer as "fptr"

Do you really think this comment contains any useful information?
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){

You already read the records into patient back in main. Why are you
reading them again?
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);


You write the fields of the record with no intervening characters
between them. How is your call to fscanf supposed to know where
forename ends and initial begins?
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);


Ditto between surname and both integer fields or between day_of_entry
and max_wait.
fprintf(fptr,"%d ",patient[20].id);

patient[20] still does not exist.
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++){

Why n+1? This insures you will process extraneous data. Fortunately
(or un-), the data is initialized since patient is at file scope.

Did you mean n-1? That would make sense but the common idiom is
i < n, not i <= n-1.
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;
}

Remove del for email



So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris
 
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.

Are we supposed to guess what you are talking about? What did you
want? What actually happened?




#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

You should verify that fopen succeeded.
//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){

This is the wrong test. fscanf returns EOF only if no data was
converted. You should be proceeding only if the return value is 6.

You never check for n overflowing the number of elements in your
array.


}
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){

You never use limit so why do you care what it's value is.
list_queue();

There is no prototype in scope for this function.
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: ");

On a buffered system, calls to printf that don't end with a '\n' may
not appear before the system waits for input. If you want the input
to appear on the same line as the prompt, you should add
fflush(stdout);
to insure the buffer is flushed to the stream.
scanf("%d", &patient[20].id);

Every use of patient[20] invokes undefined behavior. The valid
subscripts are 0 through 19 (MAXPATIENTS-1).
printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.
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]);

While it is perfectly legal to pass a struct, the common
recommendation is to pass a pointer to the struct if the structure is
larger that trivial.


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) ;

There is no prototype in scope for this function. You also forgot to
provide the definition of this function.
int x;
int error;
FILE * fptr; //Declares file pointer as "fptr"

Do you really think this comment contains any useful information?
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){

You already read the records into patient back in main. Why are you
reading them again?
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);


You write the fields of the record with no intervening characters
between them. How is your call to fscanf supposed to know where
forename ends and initial begins?
fprintf(fptr,"%s ",patient.surname);
fprintf(fptr,"%d ",patient.day_of_entry);
fprintf(fptr,"%d \n",patient.max_wait);


Ditto between surname and both integer fields or between day_of_entry
and max_wait.
fprintf(fptr,"%d ",patient[20].id);

patient[20] still does not exist.
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++){

Why n+1? This insures you will process extraneous data. Fortunately
(or un-), the data is initialized since patient is at file scope.

Did you mean n-1? That would make sense but the common idiom is
i < n, not i <= n-1.
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;
}

Remove del for email



So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris
 
O

osmium

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.

Are we supposed to guess what you are talking about? What did you
want? What actually happened?




#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;
So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait

You start by either fixing the things Barry mentioned or state why his
objections were not valid. Then, post that modified, indented, code, with a
question inserted somewhere in the post.
 
C

chutsu

On 28 May 2007 12:14:33 -0700, "(e-mail address removed)" <[email protected]>
wrote:
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.
Are we supposed to guess what you are talking about? What did you
want? What actually happened?
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;

So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait

You start by either fixing the things Barry mentioned or state why his
objections were not valid. Then, post that modified, indented, code, with a
question inserted somewhere in the post.


printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.

How do I prevent this from ending prematurely?? by changing
&patient[20].initial to &patient[19].initial ??
 
O

osmium

On 28 May 2007 12:14:33 -0700, "(e-mail address removed)" <[email protected]>
wrote:
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.
Are we supposed to guess what you are talking about? What did you
want? What actually happened?

#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;

So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait

You start by either fixing the things Barry mentioned or state why his
objections were not valid. Then, post that modified, indented, code, with
a
question inserted somewhere in the post.


printf("Forename: ");
scanf("%s",
patient[20].forename);
printf("Middle Initial: ");
scanf("%c ",
&patient[20].initial);

This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.

How do I prevent this from ending prematurely?? by changing
&patient[20].initial to &patient[19].initial ??

You can read two keys by

scanf("%c%c", &a, &junk) ;

where a and junk are variables of type char.

Note that your quoting mechanism is messed up.
 
C

chutsu

On 28 May 2007 12:14:33 -0700, "(e-mail address removed)" <[email protected]>
wrote:
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.
Are we supposed to guess what you are talking about? What did you
want? What actually happened?
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;
<snip>
So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
You start by either fixing the things Barry mentioned or state why his
objections were not valid. Then, post that modified, indented, code, with
a
question inserted somewhere in the post.
printf("Forename: ");
scanf("%s",
patient[20].forename);
printf("Middle Initial: ");
scanf("%c ",
&patient[20].initial);
This will cause problems with any following calls to scanf. To enter
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate
the next scanf prematurely.
How do I prevent this from ending prematurely?? by changing
&patient[20].initial to &patient[19].initial ??

You can read two keys by

scanf("%c%c", &a, &junk) ;

where a and junk are variables of type char.

Note that your quoting mechanism is messed up.

What do you mean by my quoting mechanism??
 
B

Barry Schwarz

On 28 May 2007 14:35:43 -0700, "(e-mail address removed)" <[email protected]>
wrote:


snip 240 lines of irrelevant history
So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris

Posting the same message every 10-20 minutes only serves to get you
ignored.


Remove del for email
 
K

Keith Thompson

Barry Schwarz said:
On 28 May 2007 14:35:43 -0700, "(e-mail address removed)" <[email protected]>
wrote:
snip 240 lines of irrelevant history
So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris

Posting the same message every 10-20 minutes only serves to get you
ignored.

I've seen a lot of that kind of thing recently, all of it from Google
Groups users. It may not be entirely the OP's fault.
 
O

osmium

What do you mean by my quoting mechanism??

The paragraph above that starts "this will cause" was written by Barry
Shwartz. The post I responded to had no > so it looked to me like something
*you* had written. Look at the post you made preceding this one.
 
B

Barry Schwarz

Barry Schwarz said:
On 28 May 2007 14:35:43 -0700, "(e-mail address removed)" <[email protected]>
wrote:
snip 240 lines of irrelevant history
So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait
Thanks
Chris

Posting the same message every 10-20 minutes only serves to get you
ignored.

I've seen a lot of that kind of thing recently, all of it from Google
Groups users. It may not be entirely the OP's fault.

When several messages have the same timestamp, I'm willing to accept
that the user thought it didn't go through. When it's four messages
spread out pretty evenly over 40 minutes, I'll bet the sender thinks
he is in a chat room.


Remove del for email
 
M

Mark McIntyre

On 28 May 2007 13:56:09 -0700, in comp.lang.c , "(e-mail address removed)"

(snip 250 lines)
So how do I avoid the program from skipping when I try to type in the
max_wait, in patient[20].max_wait

You reposted the /entire/ article, just to add a small question .
Please learn to trim articles down when following up.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

David Thompson

printf("Forename: ");
scanf("%s", patient[20].forename);
printf("Middle Initial: ");
scanf("%c ", &patient[20].initial);

This will cause problems with any following calls to scanf. To enter

No it won't -- or not for the reason you state; all the out-of-bounds
references to patient[20] are U.B. as already noted several times.

Also, I haven't seen anyone mention it yet (although I may have missed
it), but it is U.B. if you use %s for a char array of size N and the
input contains contiguous nonwhitespace of more than N-1 characters.
To be safe you should use e.g. %19s for a char[20] data field.
Or better, fgets() into a nice long line buffer and check whether the
value overflows your storage space, and give a nice clear error
message and/or allow retry. Or better yet, use a data structure that
allows for longer values, or variable-length (dynamic) values. (Either
or both of which are actually useful lessons to learn.)
the initial, you will have to press two keys, one for the letter and
one for ENTER. This will result in two characters in the input
stream, one for the letter and a '\n' for the ENTER. The %c will only
consume the letter. The '\n' will stay in the stream and terminate

Those are true.
the next scanf prematurely.
That's not. The trailing space in the format string "%c " will skip
ALL whitespace -- including but not limited to the newline.

Alternatively, if the next scanf is any format specifier other than c
or [...] (or doubled % if you count that as a specifier) then it will
skip _leading_ whitespace. In the code upthread the next scanf is in
fact "%s" for "Surname" which does skip whitespace.
How do I prevent this from ending prematurely?? by changing
&patient[20].initial to &patient[19].initial ??

Moot.

Aside: Quite a lot of people have names that don't fit the rigid mold
the program upthread demands. If this were a real system and not just
homework, and your system denied medical treatment to people because
of their names, you would be in BIG trouble -- probably in jail.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top