i have 2 problems. i will only post the first one for now:
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" );
printf( "\n" );
}
/****************** attempt to fill database **************************/
void load_family ( void ) {
int i ;
char choice[BUFSIZ];
printf( "%senter the all the names of your family.\n" , name );
printf( "hit enter after each name ( c to continue ) : \n");
for ( i = 0; i < MAX; ++i) {
fgets( choice, sizeof choice , stdin );
sscanf ( choice , "%s" , &tree[i].name );
if ( choice[0] == 'c' )
load_ages();
}
}
/************** load ages function into database*************************//////
void load_ages ( void ) {
int i ;
char choice[BUFSIZ];
printf( "\n%sNow we will enter the ages of the family you entered!.\n" , name );
printf( "hit enter after each age ( c to continue ) :\n ");
for ( i = 0; i < MAX; ++i) {
printf ( "family member : %s \n", tree[i].name );
puts("enter the age now!");
fgets ( choice, sizeof ( choice ) , stdin );
sscanf( choice , "%i" , &tree[i].age);
if ( choice[0] == 'c' )
display_menu();
}
}
void display_menu( void ) {
char number[BUFSIZ];
int choice;
printf( "\ncatalog search database:\n" );
printf( " 1. search by name\n" );
printf( " 2. display family tree\n" );
printf( " 3. quit the program\n" );
fgets( number , sizeof number, stdin );
sscanf ( number, "%i", &choice );
printf("%i ", choice);
switch ( choice ) {
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 ( c to continue ) :
padme
leia
anakin
c
luke
Now we will enter the ages of the family you entered!.
hit enter after each age ( c to continue ) :
family member : padme
enter the age now!
17
family member : leia
enter the age now!
21
family member : anakin
enter the age now!
38
family member : c
enter the age now!
9
family member :
enter the age now!
c
catalog search database:
1. search by name
2. display family tree
3. quit the program
2
2 the names and there ages in your family tree are :
padme , 17
leia , 21
anakin , 38
c , 9
, 0
, 0
, 0
, 0
family member :
enter the age now!
i deleted a million , 0 so it would be shorter!
its picking up the c what would work to move on without having the user enter something?
and of course the loop problem!