initialize the i?
initialize the i?
this should process all the names?
it still crashesCode:void load_ages ( void ) { int i ; char choice[BUFSIZ]; printf( "\n%sEnter the ages of the family you entered!.\n" , name ); printf( " ( q to stop ) :\n "); do { printf ( "family member : %s \n", tree[i].name ); } while ( choice[0]!= 'q' ); }
read thru my sentence and think...
hint : wat's the value of i when you call tree[i].name
ok this works but im stuck with the for loop im not happy with! also its picking up the q how do i fix that?
output:Code: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 ( q 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] == 'q' ) display_menu(); } }
Code:what is your name : luke ********************************************* ** welcome to the family catolog ** Family tree menu: 1. Enter names 2. Enter ages 3. display family tree 4. Search family member 5. quit the program 1 luke enter the all the names of your family. hit enter after each name ( q to stop ) : padme 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! 23 family member : leia enter the age now! 12 family member : q enter the age now! 1 family member : enter the age now!
Why are you so stupid? If you want it to stop as soon as you enter q, then don't ask it for age before you check for q! Come on man, think! This thread shouldn't be four pages long, and wouldn't be, if you actually thought about what you're trying to do, versus what you're really doing.
Quzah.
Hope is the first step on the road to disappointment.
this thingCode: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 ( q to stop ) : \n"); i = 0; while( fgets( choice, sizeof choice , stdin) != NULL ) { sscanf ( choice , "%s" , tree[i].name ); if ( choice[0] == 'q' || i == MAX) load_ages(); // go to the load_ages function ++i; } }an obvious good question : do i want to store the name first, or check the validity of the input first?Code:while( fgets( choice, sizeof choice , stdin) != NULL ) { sscanf ( choice , "%s" , tree[i].name ); if ( choice[0] == 'q' || i == MAX) load_ages(); // go to the load_ages function ++i; }
@thread starterOriginally Posted by quzah
don't take that as a form of offence even if he intended to do so... if i were you, i'd take that as a good point
i didnt even see his response, but i feel he is right.why am i so stupid? who knows lol maybe i dont belong here anymore! learning c is not easy and its easy to make mistakes. maybe im not smart enough to be doing this!
Last edited by mrsirpoopsalot; 10-14-2006 at 09:15 AM.
the thing is not whether you are smart enough, it's your tries that count...
I have learned C for about a year now and I sometime still make some silly mistakes... so don't worry too much about making mistakes, worry about learning from them
Last edited by yxunomei; 10-14-2006 at 09:15 AM.
ya i see what i was doing wrong.
thanks for all your helpCode: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 ( q to stop ) : \n"); i = 0; while( fgets( choice, sizeof choice , stdin) != NULL ) { sscanf ( choice , "%s" , tree[i].name ); ++i; if ( choice[0] == 'q' || i == MAX) display_menu(); } }
much appreciated
i should stop this thread and quit trying to do this program
no you still don't get what i meant...
see, after you fgets (reading the user's input), u copy whatever the input to the designated place using sscanf...
maybe it's good if you actually test the validity of the input (ie, whether the user type in 'q'), and if so, you don't want to sscanf it rite?
Code:while( fgets( choice, sizeof choice , stdin) != NULL ) { sscanf ( choice , "%s" , tree[i].name ); // put everything in the struct or if ( choice[0] == 'q' || i == MAX) // checking the validity of input first? display_menu(); ++i;
Last edited by yxunomei; 10-14-2006 at 09:33 AM.
your right i should have known that!
but for some reason i didnt see that
=( i can feel your frustration and mine.
nah, i'm having fun... C is fun
if i got this wrong im going to poke my eye out
quzah is right i need to think a little harderCode: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 ( q to stop ) : \n"); i = 0; while( fgets( choice, sizeof choice , stdin) != NULL ) { if ( choice[0] == 'q' || i == MAX) { printf("we will now enter there ages\n"); load_ages(); } sscanf ( choice , "%s" , tree[i].name ); ++i; } }
im having fun too dispite the frustration. i love c and im not ever going to quit
yep that's good enuff... one tips, do indentation...