im trying to ask the user to enter there family names and ages into a database
output:Code:#include <stdio.h>
#include <stdlib.h>
#define MAX 100
/* proto functions *******************/
void print( void );
void load_family ( void );
void load_ages ( void );
/**********************************/
struct family { // family database
char name[BUFSIZ];
char gender[BUFSIZ];
int 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 stop ) : \n");
for ( i = 0; i < MAX; ++i) {
fgets ( tree[i].name , sizeof ( name ), stdin );
ch = getche();
if ( ch == 'q' )
load_ages();
}
}
/************** load ages function into database*************************//////
void load_ages ( void ) {
int i ;
char ch;
printf( "\n%s Now we will enter the ages of the family you entered!.\n" , name );
printf( "hit enter after each age ( q to stop ) :\n ");
for ( i = 0; i < MAX; ++i) {
printf ( "family member : %s ", tree[i].name );
puts("enter the age now!");
scanf ("%s" , &tree[i].age );
ch = getche();
if ( ch == 'q' )
break;
}
}
as you can see i have to hit enter twice and it leaves off the first character in the name.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 stop ) :
anakin
padme
leia
q
luke
Now we will enter the ages of the family you entered!.
hit enter after each age ( q to stop ) :
family member : anakin
enter the age now!
23
family member : adme
enter the age now!
17
family member : eia
enter the age now!
26
family member : enter the age now!
what am i doing wrong?