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.da t", "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",&Cho ice);
printf("\n");
//Choice made, carrying out function
switch (Choice){
//New Patient
case 1:
limit = n;
if(limit < 21){
list_queue();
printf("\nPleas e 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("Forenam e: ");
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("\nPatie nt 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.da t", "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.da t", "w");
for(i=0;i<=inde x-2;i++){
fprintf(fptr,"% d ",patient[i].id);
fprintf(fptr,"% s ",patient[i].forename);
fprintf(fptr,"% c ",patient[i].initial);
fprintf(fptr,"% s ",patient[i].surname);
fprintf(fptr,"% d ",patient[i].day_of_entry);
fprintf(fptr,"% d \n",patient[i].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;
} 16 1770
On 28 May 2007 12:14:33 -0700, "ch****@gmail.c om" <ch****@gmail.c om>
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;
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.da t", "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.
> n++;
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",&Cho ice); 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("\nPleas e 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("Forenam e: "); 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("\nPatie nt 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.da t", "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.da t", "w"); for(i=0;i<=inde x-2;i++){ fprintf(fptr,"% d ",patient[i].id); fprintf(fptr,"% s ",patient[i].forename); fprintf(fptr,"% c ",patient[i].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[i].surname); fprintf(fptr,"% d ",patient[i].day_of_entry); fprintf(fptr,"% d \n",patient[i].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
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
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;
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.da t", "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.
n++;
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",&Cho ice);
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("\nPleas e 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("Forenam e: ");
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("\nPatie nt 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.da t", "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.da t", "w");
for(i=0;i<=inde x-2;i++){
fprintf(fptr,"% d ",patient[i].id);
fprintf(fptr,"% s ",patient[i].forename);
fprintf(fptr,"% c ",patient[i].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[i].surname);
fprintf(fptr,"% d ",patient[i].day_of_entry);
fprintf(fptr,"% d \n",patient[i].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
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
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;
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.da t", "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.
n++;
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",&Cho ice);
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("\nPleas e 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("Forenam e: ");
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("\nPatie nt 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.da t", "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.da t", "w");
for(i=0;i<=inde x-2;i++){
fprintf(fptr,"% d ",patient[i].id);
fprintf(fptr,"% s ",patient[i].forename);
fprintf(fptr,"% c ",patient[i].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[i].surname);
fprintf(fptr,"% d ",patient[i].day_of_entry);
fprintf(fptr,"% d \n",patient[i].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
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
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;
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.da t", "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.
n++;
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",&Cho ice);
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("\nPleas e 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("Forenam e: ");
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("\nPatie nt 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.da t", "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.da t", "w");
for(i=0;i<=inde x-2;i++){
fprintf(fptr,"% d ",patient[i].id);
fprintf(fptr,"% s ",patient[i].forename);
fprintf(fptr,"% c ",patient[i].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[i].surname);
fprintf(fptr,"% d ",patient[i].day_of_entry);
fprintf(fptr,"% d \n",patient[i].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
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
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;
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.da t", "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.
n++;
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",&Cho ice);
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("\nPleas e 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("Forenam e: ");
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("\nPatie nt 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.da t", "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.da t", "w");
for(i=0;i<=inde x-2;i++){
fprintf(fptr,"% d ",patient[i].id);
fprintf(fptr,"% s ",patient[i].forename);
fprintf(fptr,"% c ",patient[i].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[i].surname);
fprintf(fptr,"% d ",patient[i].day_of_entry);
fprintf(fptr,"% d \n",patient[i].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
<ch****@gmail.c omwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
>On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om> 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.
On 28 May, 22:37, "osmium" <r124c4u...@com cast.netwrote:
<chu...@gmail.c omwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
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("Forenam e: ");
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 ??
<ch****@gmail.c omwrote:
On 28 May, 22:37, "osmium" <r124c4u...@com cast.netwrote:
><chu...@gmail. comwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote: On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om> 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("Forenam e: ");
> 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.
On May 29, 12:43 am, "osmium" <r124c4u...@com cast.netwrote:
<chu...@gmail.c omwrote:
On 28 May, 22:37, "osmium" <r124c4u...@com cast.netwrote:
<chu...@gmail.c omwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl .netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.c om" <chu...@gmail.c om>
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("Forenam e: ");
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?? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from software giant such as Microsoft SQL
Server, Oracle, and Sybase?
Is PostgreSQL reliable enough to be used for high-end commercial
application? Thanks
|
by: titan0111 |
last post by:
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
using namespace std;
class snowfall
{
private:
int ft;
|
by: E. Robert Tisdale |
last post by:
What makes a good C/C++ programmer?
Would you be surprised if I told you that
it has almost nothing to do with your knowledge of C or C++?
There isn't much difference in productivity, for example,
between a C/C++ programmers with a few weeks of experience
and a C/C++ programmer with years of experience.
You don't really need to understand the subtle details
or use the obscure features of either language
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode support IDEs are DreamWeaver 8 and
Zend PHP Studio.
DreamWeaver provides full support for Unicode. However, DreamWeaver is
a web editor rather than a PHP IDE. It only supports basic IntelliSense
(or code completion) and doesn't have anything...
|
by: Cherrish Vaidiyan |
last post by:
Frinds,
Hope everyone is doing fine.i feel pointers to be the most toughest
part in C. i have just completed learning pointers & arrays related
portions. I need to attend technical interview on C. wat type of
questions should be expected? Which part of C language do the staff
give more concern?
The interviewers have just mentioned that .. i will have interview on
C.
Also can anyone can help me with sites where i can go thru sample
| |
by: Keith K |
last post by:
Having developed with VB since 1992, I am now VERY
interested in C#. I've written several applications with
C# and I do enjoy the language.
What C# Needs:
There are a few things that I do believe MSFT should do to
improve C#, however.
I know that in the "Whidbey" release of VS.NET currently
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: Pyenos |
last post by:
import cPickle, shelve
could someone tell me what things are wrong with my code?
class progress:
PROGRESS_TABLE_ACTIONS=
DEFAULT_PROGRESS_DATA_FILE="progress_data"
PROGRESS_OUTCOMES=
|
by: Siong.Ong |
last post by:
Dear all,
my PHP aims to update a MySQL database by selecting record
one by one and modify then save.
Here are my PHP, but I found that it doesnt work as it supposed to be,
for example, when Record (i) is shown and modified, the change will
come to Record (i+1).
Can anyone provide suggestion?
thanks.
|
by: MU |
last post by:
Hello
I have some code that sets a dropdownlist control with a parameter
from the querystring. However, when the querystring is empty, I get
an error.
Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |