Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

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


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;

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?

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.

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.

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
On 28 May, 21:23, Barry Schwarz <schwa@doezl.net> wrote:

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.net> wrote:

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.net> wrote:

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.net> wrote:

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

<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@comcast.net> 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
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@comcast.net> wrote:

What do you mean by my quoting mechanism??
On 28 May 2007 14:35:43 -0700, "chu@gmail.com" <chu@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

chu@gmail.com wrote:
> On May 29, 12:43 am, "osmium" <r124c4u@comcast.net> wrote:

>> 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.

Barry Schwarz <schwa@doezl.net> writes:
> On 28 May 2007 14:35:43 -0700, "chu@gmail.com" <chu@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) k@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"

<chu@gmail.com> wrote:
>> > 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.
On Mon, 28 May 2007 18:33:52 -0700, Keith Thompson <k@mib.org>
wrote:

>Barry Schwarz <schwa@doezl.net> writes:
>> On 28 May 2007 14:35:43 -0700, "chu@gmail.com" <chu@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

On 28 May 2007 13:56:09 -0700, in comp.lang.c , "chu@gmail.com"

<chu@gmail.com> wrote:

(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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc