By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[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;
}

May 28 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On 28 May 2007 12:14:33 -0700, "ch****@gmail.com" <ch****@gmail.com>
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.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.
> 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",&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[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
May 28 '07 #2

P: n/a
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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.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.
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",&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[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

May 28 '07 #3

P: n/a
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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.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.
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",&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[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

May 28 '07 #4

P: n/a
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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.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.
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",&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[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

May 28 '07 #5

P: n/a
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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.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.
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",&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[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

May 28 '07 #6

P: n/a
<ch****@gmail.comwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
>On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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.
May 28 '07 #7

P: n/a
On 28 May, 22:37, "osmium" <r124c4u...@comcast.netwrote:
<chu...@gmail.comwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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 ??

May 28 '07 #8

P: n/a
<ch****@gmail.comwrote:
On 28 May, 22:37, "osmium" <r124c4u...@comcast.netwrote:
><chu...@gmail.comwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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.
May 28 '07 #9

P: n/a
On May 29, 12:43 am, "osmium" <r124c4u...@comcast.netwrote:
<chu...@gmail.comwrote:
On 28 May, 22:37, "osmium" <r124c4u...@comcast.netwrote:
<chu...@gmail.comwrote:
On 28 May, 21:23, Barry Schwarz <schwa...@doezl.netwrote:
On 28 May 2007 12:14:33 -0700, "chu...@gmail.com" <chu...@gmail.com>
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??

May 29 '07 #10

P: n/a
On 28 May 2007 14:35:43 -0700, "ch****@gmail.com" <ch****@gmail.com>
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
May 29 '07 #11

P: n/a
ch****@gmail.com wrote:
On May 29, 12:43 am, "osmium" <r124c4u...@comcast.netwrote:
>>
Note that your quoting mechanism is messed up.

What do you mean by my quoting mechanism??
That last post was a good example, you quoted the entire message to
respond to one line. Learn to trim.

--
Ian Collins.
May 29 '07 #12

P: n/a
Barry Schwarz <sc******@doezl.netwrites:
On 28 May 2007 14:35:43 -0700, "ch****@gmail.com" <ch****@gmail.com>
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 29 '07 #13

P: n/a
<ch****@gmail.comwrote:
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.
>Note that your quoting mechanism is messed up.

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.
May 29 '07 #14

P: n/a
On Mon, 28 May 2007 18:33:52 -0700, Keith Thompson <ks***@mib.org>
wrote:
>Barry Schwarz <sc******@doezl.netwrites:
>On 28 May 2007 14:35:43 -0700, "ch****@gmail.com" <ch****@gmail.com>
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
May 29 '07 #15

P: n/a
On 28 May 2007 13:56:09 -0700, in comp.lang.c , "ch****@gmail.com"
<ch****@gmail.comwrote:

(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
May 29 '07 #16

P: n/a
On 28 May 2007 15:37:01 -0700, "ch****@gmail.com" <ch****@gmail.com>
wrote:
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
Jul 1 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.