Thread: passing structures

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    passing structures

    Hi!

    I have a structure defined as follows:

    Code:
    struct	passenger
    {
    int flag;  //Use as a boolean - 1 if seat taken, 0 if not taken
    char first[NAME];
    char last[NAME];
    };
    
    typedef struct passenger pass;
    And I pass it to a function to modify the contents - name and last :

    Code:
    void option_c(pass cust[][LABEL])
    {	char ans[3], label;
    	int row;
    
    	printf("Enter the seat number you wish to edit: ");
    	
    	seat_check(&row, &label, ans);
    
    	if (cust[row][label].flag)
    		printf("blah blah balh");
    
    	else
    	{
                                    printf("Enter first:");
    		scanf("%[^\n]", cust[row][label].first);
    		fflush(stdin);
                                    printf("Enter second:");
    		scanf("%[^\n]", cust[row][label].last);
    		fflush(stdin);
                    }
    }
    The problem lies in the else. For some unknown reason - the program will crash at this point

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    fflush(stdin);
    *gasp*

    Why fflush(stdin) is wrong

    Where exactly does it crash? On the scanf()s? What are row and label set to by seat_check()? 12273849?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    else
    {
            printf("Enter first:");
            fgets(cust[row][label].first, NAME, stdin);
    		/*scanf("%[^\n]", cust[row][label].first);
    		fflush(stdin);*/
    		
    		
            printf("Enter second:");
    		fgets(cust[row][label].first, NAME, stdin);
    		/*scanf("%[^\n]", cust[row][label].last);
    		fflush(stdin);*/
    
    }
    use fgets

    ssharish2005

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Don't forget to remove the newline. From http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    int main()
    {
      char buf[BUFSIZ];
      char *p;
      
      printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        printf ("Thank you, you entered >%s<\n", buf);
        
        /*
         *  Now test for, and remove that newline character
         */
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
          
        printf ("And now it's >%s<\n", buf);
      }
      
      return 0;
    }
    
    
    /*
     Program output:
    
    Please enter a line of text, max 512 characters
    this is a test
    Thank you, you entered >this is a test
    <
    And now it's >this is a test<
    */
    I seriously doubt that's the problem though. I bet the values of row or label are wacky.
    Last edited by dwks; 12-13-2006 at 05:57 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  5. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM