need some help

This is a discussion on need some help within the C Programming forums, part of the General Programming Boards category; Nope. fgets reads at most size-1 characters or until encountering a newline. If there's enough room, the newline gets put ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Nope. fgets reads at most size-1 characters or until encountering a newline. If there's enough room, the newline gets put in the buffer, otherwise it doesn't. Anyway I hoped this would demonstrate that you could always just check the first character of the buffer for your quit ...signal... thingy.[/technical word]

    Of course if somebody entered
    tom
    quincy
    harry
    The loop would quit too soon, so I would just keep reading until a blank line is entered (a buffer containing \n as the first character).

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

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Follow my original suggestion. Read on until a blank line is entered.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mrsirpoopsalot
    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!
    Actually, probably 100. Which would be how many times you are telling it to loop.
    Code:
    #define MAX 100
    /* ... */
    for ( i = 0; i < MAX; ++i) {
    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.*

  5. #20
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Code:
    Actually, probably 100. Which would be how many times you are telling it to loop.
    ya i found that out when i checked out the code and realized i didnt need that loop, i dont know why i used it . <sigh>

    citizen:
    Follow my original suggestion. Read on until a blank line is entered.
    sorry for my stupidity but i dont understand how i could do that in my code. im thinking this is just a big debacle and i should just forget this whole mess! maybe im in over my head!

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mrsirpoopsalot
    sorry for my stupidity but i dont understand how i could do that in my code. im thinking this is just a big debacle and i should just forget this whole mess! maybe im in over my head!
    No, early on it's frustrating. But it is far better to have these issues nailed down before moving forward.

    So ask yourself, what would an empty line be? One that contains nothing, "", or only a newline "\n" (if you aren't always snipping the trailing newline). That would be the point at which you can quit incrementing a counter. And that final counter value is what you want to loop about.
    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.*

  7. #22
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    i will attempt to improve it in a couple of days and repost. thanks dave

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Quote Originally Posted by mrsirpoopsalot
    i will attempt to improve it in a couple of days and repost. thanks dave
    Yeah. That's the spirit man. Don't let me make you upset: we are all just trying to help. I think I have a bit of a stress problem that causes anything I write or say to strike people with a generically blunt object.

  9. #24
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    citizen
    Perfect Gentleman
    Heh...


    Sir, just keep chuggin on, you are hitting all the snags that most people hit when they first start programing. If you keep at it and have some patience, you should do fine .

  10. #25
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thanks for all the encouragement!

  11. #26
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    where i seem to be having problems now is the search function!
    it just simply wont work.
    Code:
     /*********** search name function ********/              
    void search_name ( void ) {
         int found = 0 , i;  
         char choice[BUFSIZ];        
         printf( "name: ");
         for ( i = 0; i < MAX; ++i )
         fgets ( choice , sizeof choice , stdin );
         //sscanf( choice , "%d" , tree[i].name );
         if ( !strcmp ( choice , 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 );
         
    }
    im also trying to pass the name to display_catalog()
    is it correct?
    thanks for all your help people i really appreciate it.

  12. #27
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I'm sorry, Wraithan. I shall improve my attitude then.

    So the search function doesn't work. What happens when you call it? Anything specific?

  13. #28
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    i get no errors nothing. it just does nothing! i dont know

  14. #29
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    From your latest posted code, I still find this:
    Code:
    for ( i = 0; i < MAX; ++i )
    It's in all functions still, rather than being the number of items entered. Wading through all that excess garbage couldn't get me to your search function.

    But perhaps you are comparing text with a newline versus that without. It would help if you followed the advice in the FAQ/tutorial I pointed you to.
    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.*

  15. #30
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    if i knew how to do this i would! thats why you see a max because its all i know how to do =(
    Quote Originally Posted by Dave_Sinkula
    From your latest posted code, I still find this:
    Code:
    for ( i = 0; i < MAX; ++i )
    It's in all functions still, rather than being the number of items entered. Wading through all that excess garbage couldn't get me to your search function.

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21