File input and output added to name struct
/*** the appropriated headers are missing ***/
#include "name_struct.h"
void fill_name(NAME *person)
{
printf("\nEnter first name (maximum of 15 characters) ");
my_gets((*person).first, 15);
printf("\nEnter last name (maximum of 15 characters) ");
my_gets((*person).last, 15);
}
void file_my_gets(FILE *in, char strng[], int size)
{
/* this is NOT robust, you should "do something about that", usually,
you don't have the same problems reading from a file that you have
reading from the keyboard since you "know" what the file looks like.
For example, you may know that no first name is larger than 10
characters and no last name has more than 25 characters.
*/
fscanf(in,"%s", strng);
}
void fill_name_file(NAME *person, FILE *in)
{
file_my_gets(in, (*person).first, 15);
file_my_gets(in, (*person).last, 15);
}
void print_name_lastfirst(NAME person)
{
printf("%s, %s", person.last, person.first);
}
void file_print_name_lastfirst(FILE *out, NAME person)
{
fprintf(out, "%s, %s", person.last, person.first);
}
void print_name_firstlast(NAME person)
{
printf("%s %s", person.first, person.last);
}
void swap_name(NAME *one, NAME *two)
{
NAME temp;
temp = *one;
*one = *two;
*two = temp;
}
int compare_names(NAME one, NAME two)
{
int value;
value = strcmp(one.last, two.last);
if (value == 0)
value = strcmp(one.first, two.first);
return (value);
}
int main(void)
{
NAME mine = {"Brenda", "Sonderegger"};
NAME yours;
printf("\nEnter a name ");
fill_name(&yours);
if (compare_names(mine, yours) > 0)
print_name_firstlast(yours);
else
print_name_lastfirst(mine);
return(0);
}