Thread: need some help

  1. #31
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Oh, something along this line:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    size_t array_get(char (*array)[20], size_t size);
    void array_print(char (*array)[20], size_t size);
    
    int main(void)
    {
       char array[10][20];
       size_t count = array_get(array, sizeof array / sizeof *array);
       array_print(array, count);
       return 0;
    }
    
    size_t array_get(char (*array)[20], size_t size)
    {
       char text[20];
       size_t i = 0;
       while ( i < size )
       {
          printf("enter some text: ");
          fflush(stdout);
          if ( fgets(text, sizeof text, stdin) &&
               sscanf(text, "%19s", array[i]) == 1 )
          {
             ++i;
          }
          else
          {
             break;
          }
       }
       return i;
    }
    
    void array_print(char (*array)[20], size_t size)
    {
       while ( size-- )
       {
          puts(*array++);
       }
    }
    
    /* my output
    enter some text: one
    enter some text: two
    enter some text: three
    enter some text: 
    one
    two
    three
    */
    Last edited by Dave_Sinkula; 10-12-2006 at 10:19 PM. Reason: Changed from integer input to text.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #32
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Quote Originally Posted by Dave_Sinkula

    {

    if ( fgets(text, sizeof text, stdin) &&
    sscanf(text, "%19s", array[i]) == 1 )
    is this saying that the condition of if is false exit out ?
    0= true
    1 = false?

  3. #33
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you successfully got text and then successfully put it into array[i], happily continue on.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #34
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thank you Dave_Sinkula
    for all your help!
    i try to take all your advice , but sometimes it goes over my head. i tried to read up on size_t and when its a good time to use it but i cant find any good tutorials on the web for it!

  5. #35
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    size_t [...] is the unsigned integer type of the result of the sizeof operator
    It's also the result of strlen and others.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #36
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    this causes the program to crash without error:
    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 ");
         printf ( "family member :  %s \n", tree[i].name );
         puts("enter the age now!");
         while ( choice[0]!= 'q' ) {
         if ( fgets ( choice , sizeof choice  , stdin ) &&
              sscanf( choice , "%s" , tree[i].age)== 1 );
         
         //tree[i].age = atol( choice ); 
        // if ( choice[0] == 'q' )
        // display_menu();
    }
    }
    i think i narrowed it down to the printf is the function that adds the names not processing it right?
    and thats why it crashes when i print it?
    Last edited by mrsirpoopsalot; 10-14-2006 at 06:54 AM.

  7. #37
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    try
    Code:
    if ( fgets (choice, sizeof(choice), stdin) != NULL ...

  8. #38
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That's not different.

    Are you sure that is where you crash, sir? I see something else:
    sscanf( choice , "%d" , &tree[i].age)
    Last edited by whiteflags; 10-14-2006 at 07:21 AM.

  9. #39
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    ah yes, it's not different in this case... fgets there will pass a pointer value, that will never be 0

    i juz realized
    Code:
         if ( fgets ( choice , sizeof choice  , stdin ) &&
              sscanf( choice , "%s" , tree[i].age)== 1 );
    wat's with ";" after the if() ?
    Last edited by yxunomei; 10-14-2006 at 07:26 AM.

  10. #40
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    im having problems with processing the names right. i think thats whats causing the crash
    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");
         for ( i = 0; i < MAX; ++i) {
            fgets( choice, sizeof choice , stdin );
            sscanf ( choice , "%s" ,  &tree[i].name );
         if ( choice[0] == 'q' ) //doesnt work
         
         printf("family = %s", tree[i].name ); //just to check
         //load_ages();
         
    }
    any suggestions that i can understand?
    Last edited by mrsirpoopsalot; 10-14-2006 at 07:44 AM.

  11. #41
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    i'm not sure, have u actually done something if the user put 'q' ?
    Code:
    if ( choice[0] == 'q' ) //doesnt work
    if you want to terminate the program, u can put something like
    Code:
    if ( choice[0] == 'q' )
       return; // return with no value, merely exit the program

  12. #42
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    u don't neccessarily need the "&" ... since name itself is an array, it will always get passed to sscanf and also the printf... don't use & infront of the printf 2nd argument
    Code:
    printf("family = %s", &tree[i].name ); //just to check
    Code:
    sscanf ( choice , "%s" ,  &tree[i].name );
    Last edited by yxunomei; 10-14-2006 at 08:07 AM.

  13. #43
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    ok it works but picks up the q
    but i cant figuare out a way to get rid of the for loop and do it a different way like a if and combine the fgets & sscanf together?
    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");
         for ( i = 0; i < MAX; ++i) {
            fgets( choice, sizeof choice , stdin );
            sscanf ( choice , "%s" ,  &tree[i].name );
         if ( choice[0] == 'q' ) 
         for ( i =0; i< 3; ++i)
         printf("%s", &tree[i].name ); //just to check
         //load_ages();
         }
    }
    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
    q
    padme
    q
    Last edited by mrsirpoopsalot; 10-14-2006 at 08:17 AM.

  14. #44
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    eh? sorry wat exactly do you want to do? you can use
    Code:
    i = 0;
    while( fgets( choice, sizeof choice , stdin) != NULL ) { 
       sscanf ( choice , "%s" ,  tree[i].name );
       if ( choice[0] == 'q' || i == MAX) 
          return;
       ++i;
    }
    if you don't like for loop that much


  15. #45
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    ok i think it works loading the names but when i go to load in the ages it crashes
    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;
         }
         
    
         
    }
    hers where it crashes just trying to print name
    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 ");
         printf ( "family member :  %s \n", tree[i].name ); //i think it crashes here
         /*
         puts("enter the age now!");
         while ( choice[0]!= 'q' ) {
             fgets ( choice , sizeof choice  , stdin );
             tree[i].age = atol( choice );
             
         if ( fgets ( choice , sizeof choice  , stdin ) &&
              sscanf( choice , "%s" , tree[i].age)== 1 );
         */
        
        
        // display_menu();
    //}
    }

Popular pages Recent additions subscribe to a feed