Functions of Program do not execute properly 
Author Message
 Functions of Program do not execute properly

The following function in conjunction with a find record function
updates records in a database one at a time by using the email
address as a key. The find function takes the email address and
compares it to all the entries alreay in the database.

When it finds the email address it returns it's index.

First off a function that was working fine with
gets now exits prematurely, It does every thing
it's supposed to do until it gets to the last line
of the program. The prompt "Do you want to add
another record appears? Y/N" and then the program
exits. I'll display this function first.

In the update function below (which is just part of
the entire function) the function displays garbage.

The function though continues and prompts "is this
is the record I want to display? Y/N  twice and the cursor moves
to the next prompt "Do You Want to change the last
name? Y/N"

If I enter 'Y' I get a prompt "Enter a new first name"
I enter a name and the prompt "Do You
want to add another record appears"

If I type 'N' the program exits as it should but
if I type 'Y' the prompt "Enter an email address"
appears followed by garbage and it skips to the prompt
"Do you want to add another record? Y/N" where the
cursor now appears.

If I type 'N' I get the same behavior as before
but will reitierate the problems.

If I enter 'N' I get the prompt " Do you want
to change the last name?" If I enter 'N' it
proceeds to the next prompt " Do you want
to change the first name" I repeat this to
the prompt on phone. After phone the prompt
"Do you want to change email address appears
but the cursor goes to the next prompt " Do
you want to update another record". The same
behavior is elicited as it was above if 'Y' is
entered. But the return key has to be pressed
2 fewer times for each prompt.

Since this program uses the find function I placed
it under the update function. see comments below

Any suggestions would be appreciated.

Thanx in advance

Alan

Here is the add_record function with major components:

If I enter 'N' I get the prompt " Do you want
to change the last name?" If I enter 'N' it
proceeds to the next prompt " Do you want
to change the first name" I repeat this to
the prompt on phone. After phone the prompt
"Do you want to change email address appears
but the cursor goes to the next prompt " Do
you want to update another record". The same
behavior is elicited as it was above if 'Y' is
entered. But the return key has to be pressed
2 fewer times for each prompt.

void add_record (PERSON add_record [ ],int index)

{

int mode = ADDIT;

index = 0;

do {

fflush ( stdin );

printf ( "Enter Last Name .....   ");

fgets ( add_record [index] . lname, 30, stdin );

printf ( "Enter First Name .....   ");

fgets ( add_record [index] . fname, 30, stdin );

printf ( "Enter Address .....   ");

fgets ( add_record [index] . address, 50, stdin );

printf ( "Enter Telephone Number .....   ");

fgets ( add_record [index] . phone, 16, stdin );

printf ( "Enter email address .....   ");

fgets ( add_record [index] . email, 20, stdin );

clear_array ( add_record );

if ( index < MAX_RECORDS )

printf(" Do you want to add another record? Y/N");

else

{
printf ( "database too large now exiting");

exit ( 0 );

Quote:
}

index++;

Quote:
}while (getchar ( ) == 'Y');

save_record ( add_record , MAX_RECORDS, index, mode );

clear_array ( add_record );

display_menu( );

Quote:
}

void update_record ( PERSON update_record [ ], int update )

{

        int found;
        update = 0;
        int mode = UPDATE;
        int funct = UPDATE;

do {

printf ("Please enter an email address \n\n  ");

fgets ( update_record [update] . email, 20, stdin );

found = find_record ( update_record, MAX_RECORDS, update, funct );

printf ( "Do you want to change the last name? YES/NO ");

if (getchar == 'Y' )

/*This is where the cursor ends up displaying garbage, and Is this the record you want to update*/

{

printf ( "Enter a new last name" );
/*This prompt appears if I enter 'Y'  After I enter a name the program skips to "Do you want to update another record" */

fgets ( update_record [update] . lname, 30, stdin);

strcpy ( update_record [found] . lname,

update_record [update] . lname);

                                                        }

else
        If ( getchar ( )  == 'N' )

/* If I enter 'N' at the rest of the prompts the program runs as it should until email (see below)*/

printf ( "Do you want to change the first name? YES/NO");

if ( getchar ( ) == 'Y' )

{
printf ( "Enter a new first name" );

fgets ( update_record [update] . fname, 20, stdin);

strcpy ( update_record [found] . fname, update_record [update] .fname);
                                }                                                                      

Quote:
}

/* This function continues as above and prompts for last name, address, phone number and email
address. When it gets to email address it displays the prompt but the cursor goes to the next prompt
which is update record.*/

update ++;

printf ( "Do you want to update another record? Y/N");

/* If I hit 'N' the program exits as it should but if I hit 'Y', if I type 'Y' the prompt "Enter an email address"
appears followed by garbage and it skips to the prompt"Do you want to add another record? Y/N" where the
cursor now appears. If I enter 'N' I get the prompt " Do you want  to change the last name?" If I enter 'N' it
proceeds to the next prompt " Do you want to change the first name" I repeat this to the prompt on phone.
After phone the prompt" Do you want to change email address appears but the cursor goes to the next prompt "
Do you want to update another record". The same behavior is elicited as it was above if 'Y' is entered. But the return
 key has to be pressed 2 fewer times for each prompt. */

                }while ( getchar ( ) =='Y');

Quote:
}

Here is the find record function:

int find_record ( PERSON find_record [ ], int update, int funct )

{

int counter = 0;

FILE *fptr;

if ((fptr = fopen("records.dbf","rb")) == NULL)

{
printf ("No database file found");

exit (1);

Quote:
}

while ( counter != MAX_RECORDS)
{
if  ( strcmp ( find_record [update] . email, find_record [counter] . email ) == 0)
{
while ( fread ( &find_record [update], sizeof (find_record [update]),1, fptr ) == 1)
{
printf ( "%s\n", find_record [update] . lname );
printf ( "%s\n", find_record [update] . fname );
printf ( "%s\n", find_record [update] . address );
printf ( "%s\n", find_record [update] . phone );
printf ( "%s\n", find_record [update] . email );

 }

switch ( funct )
 {
case DISPLAY : puts ( "Is this the record you want to display? Y/N?...");

break;

 case DELETE :  puts ("Is this the record you want to delete? Y/N?...");

break;

case    UPDATE :  puts ( "Is this the record you want to update? Y/N/...");

break;

default : printf ("Invalid function call %d", funct);

break;

 }
 }
counter++;

 }

fclose ( fptr );

if ( getchar() == 'Y' )

return counter;

else

return -1;

Quote:
}

--



Fri, 11 Apr 2003 13:11:33 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. CFile not working properly, can't figure out why (last message not sent properly)

2. : when execute a program in other program by function

3. MC could not handle virtual function properly

4. The value of ESP was not properly saved across a function call

5. ESP was not properly save across a function call

6. HELP NEEDED : value of ESP was not properly saved across a function call

7. value of ESP was not properly saved across a function call

8. Value of ESP was not properly saved across a function call

9. The value of ESP was not properly saved across a function call

10. Float type not getting calculated properly in a program

11. My program does not work properly in other computers

12. ESP was not saved properly across a function call

 

 
Powered by phpBB® Forum Software