Thread: need some help

  1. #46
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    initialize the i?

  2. #47
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    this should process all the names?
    Code:
    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' );
         
    }
    it still crashes

  3. #48
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    read thru my sentence and think...

    hint : wat's the value of i when you call tree[i].name

  4. #49
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    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?
    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();
           }
    }
    output:
    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!

  5. #50
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  6. #51
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Code:
    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;
         }
         
    
         
    }
    this thing
    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;
         }
    an obvious good question : do i want to store the name first, or check the validity of the input first?

  7. #52
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by quzah
    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.
    @thread starter
    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

  8. #53
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    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.

  9. #54
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    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.

  10. #55
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    ya i see what i was doing wrong.
    Code:
    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();
         }
    }
    thanks for all your help
    much appreciated
    i should stop this thread and quit trying to do this program

  11. #56
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    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.

  12. #57
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    your right i should have known that!
    but for some reason i didnt see that
    =( i can feel your frustration and mine.

  13. #58
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    nah, i'm having fun... C is fun

  14. #59
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    if i got this wrong im going to poke my eye out
    Code:
    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;
    
         }
    }
    quzah is right i need to think a little harder
    im having fun too dispite the frustration. i love c and im not ever going to quit

  15. #60
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    yep that's good enuff... one tips, do indentation...

Popular pages Recent additions subscribe to a feed