sorry if this is sloppy or very ugly! im still new and learning. this isnt working right.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
/* proto functions *******************/
void print( void );
void load_family ( void );
void load_ages ( void );
void display_menu ( void );
void search_name ( void );
void display_catalog ( int i );
void display_catalog2();
/**********************************/
struct family { // family database
char name[BUFSIZ];
unsigned age;
} tree[BUFSIZ];
char name[BUFSIZ]; // global name
int main(int argc, char *argv[]) {
int i;
printf( "what is your name : " );
fgets ( name , sizeof ( name ), stdin );
print(); // print function to display
load_family(); // load function to load all the family names into database
getchar();
return 0;
}
/************** print function *************************/
void print ( void ) {
printf("*********************************************\n" );
printf( "%55s" ," ** welcome to the family catolog **\n" );
}
/****************** attempt to fill database **************************/
void load_family ( void ) {
int i;
char ch;
printf( "%s enter the all the names of your family.\n" , name );
printf( "hit enter after each name ( q to continue ) : \n");
for ( i = 0; i < MAX; ++i) {
fgets ( tree[i].name , sizeof ( tree[i].name ), stdin );
ch = getchar();
if ( ch == 'q' )
load_ages();
}
}
/************** load ages function into database*************************//////
void load_ages ( void ) {
int i ;
char ch;
char temp[BUFSIZ];
printf( "\n%s Now we will enter the ages of the family you entered!.\n" , name );
printf( "hit enter after each age ( q to continue ) :\n ");
for ( i = 0; i < MAX; ++i) {
printf ( "family member : %s ", tree[i].name );
puts("enter the age now!");
fgets ( temp , sizeof ( temp ) , stdin );
sscanf( temp , "%d" , &tree[i].age);
ch = getchar();
if ( ch == 'q' )
display_menu();
}
}
void display_menu( void ) {
int ch;
printf( "\ncatalog search database:\n" );
printf( " 1. search by name\n" );
printf( " 2. display family tree\n" );
printf( " 3. quit the program\n" );
ch = getchar();
switch ( ch ) {
case 1:
search_name();
break;
case 2:
display_catalog2();
break;
case 3:
exit(0);
default:
printf("wrong selection!");
getchar();
}
}
/*********** search name function ********/
void search_name ( void ) {
int found = 0 , i;
printf( "name: ");
for ( i = 0; i < MAX; ++i )
fgets ( name , sizeof name , stdin );
sscanf( name , "%d" , tree[i].name );
if ( !strcmp ( name , tree[i].name )) {
found = 1;
display_catalog(i);
}
if( !found )
puts("not found!");
}
/*VVVVVVVVVVVVVVVVVVV display catalog */
void display_catalog ( int i ) {
printf("%s\n" , tree[i].name );
printf("age : %i\n" , tree[i].age );
}
void display_catalog2() {
int i;
printf(" the names and there ages in your family tree are :\n" );
for(i = 0; i < MAX; ++i ) {
printf("%s , %i \n" , tree[i].name, tree[i].age);
}
}
output:
Code:
what is your name : luke
*********************************************
** welcome to the family catolog **
luke
enter the all the names of your family.
hit enter after each name ( q to continue ) :
padme
anakin
leia
q
luke
Now we will enter the ages of the family you entered!.
hit enter after each age ( q to continue ) :
family member : padme
enter the age now!
18
family member : nakin
enter the age now!
38
family member : eia
enter the age now!
26
family member : enter the age now!
q
catalog search database:
1. search by name
2. display family tree
3. quit the program
wrong selection!
it leaves off the first letter of there names plus catalog search database wont work!
any help is much appreciated