create and display functions

This is a discussion on create and display functions within the C Programming forums, part of the General Programming Boards category; Below is a function used to enter customer details. Under that is a function that should display the details entered ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    66

    create and display functions

    Below is a function used to enter customer details. Under that is a function that should display the details entered as a list. num_of_items_i is a global variable. When i enter details and then select yes to enter another set of details, instead of displaying them both, it only displays the last set of details i entered. Does anyone know why this is happening?

    Thankyou


    Code:
    /* ADD CUSTOMER TO CURRENT LIST */
    void f_add_customer (char *file_type)
    {
    
            int time_i;
    	char reply_c;
    
    	num_of_items_i =0;
    
    	do
    	{
    	
    	printf ("Enter Forename > ");
    		gets ( customer[num_of_items_i ].forename_s  );
    
    
            
    	printf ("Enter Surname > ");
    		gets ( customer[num_of_items_i ].surname_s  );
    
    
            printf ("Enter First line of Address > ");
    		gets ( customer[num_of_items_i ].address1_s  );
    		fflush  ( stdin );
    
            printf ("Enter Second line of Address > ");
    		gets ( customer[num_of_items_i ].address2_s  );
    
            printf ("Enter Post Code > ");
    		gets ( customer[num_of_items_i ].postcode_s  );
    
    
            printf ("Enter Telephone number > ");
    		gets ( customer[num_of_items_i ].telephone_s  );
    
    
    
    
            fflush(stdin);
    
            do
            {
              printf ("Another? Y/N > ");
              scanf ("%c", &reply_c );
              fflush(0);
    
              if (( reply_c != 'Y' ) &&  ( reply_c != 'y' ) && ( reply_c != 'N' ) && ( reply_c != 'n' ))
    
              {
                    printf ("Error. Enter Y or N only\n");
              }
             
    
    	}
    	while  (( reply_c != 'Y' ) &&  ( reply_c != 'y' ) && ( reply_c != 'N' ) && ( reply_c != 'n' ));
    
    
          } while     (( reply_c != 'N' ) && ( reply_c != 'n' ));
    
              num_of_items_i ++;
    }
    
    
    /* ************************************	*/
    /* DISPLAY CUSTOMER NAMES */
    void f_display_customer ()
    {
    	int num_i;
    	num_of_items_i =0;
    
    
    		printf("____________________________________________\n");
    		printf("           CUSTOMER INFORMATION           \n");
            	printf("____________________________________________\n");
            	printf("No Name\t\t\t\t\tAddress\t\t\tTel.No Time\n");
    
                  
                      for (num_i = 0; num_i < num_of_items_i; num_i ++);
    
    		printf ("%d %s %s	%s, %s, %s  	%s   %d\n", num_i + 1, customer[num_i].forename_s, customer[num_i].surname_s, customer[num_i].address1_s , customer[num_i].address2_s , customer[num_i].postcode_s,  customer[num_i].telephone_s,  customer[num_i].time_i );
    
    	
    		printf("___________________________________________\n\n\n\n\n");
    		}
    
    
    /* ***************************** */

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    num_of_items_i =0;
    Maybe because you're always setting it to 0 when adding a new item...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    So should i change

    num_of_items_i =0; to num_of_items_i + 1; in each function?

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    We don't recommend either fflush(stdin) or gets, see FAQ here
    and here

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First, make sure it is initialized to 0 where you declare it (globally).
    Second, remove the assignment in both functions.
    Third, leave the Nr++ in the adder function.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: create and display functions

    I see the following problems with your code which will bite you eventually or are bad form:
    Code:
    gets ( customer[num_of_items_i ].forename_s  );
    An extremely dangerous function
    ________________________

    Code:
    fflush  ( stdin );
    fflush  ( 0 );
    As linuxdude pointed out. Also 0 is an inapproriate value
    ________________________

    Code:
    scanf ("%c", &reply_c );
    A very expensive function to do such a little task. Use getchar()
    ________________________

    Code:
    printf ("%d %s %s	%s, %s, %s  	%s   %d\n", num_i + 1, customer[num_i].forename_s, customer[num_i].surname_s, customer[num_i].address1_s , customer[num_i].address2_s , customer[num_i].postcode_s,  customer[num_i].telephone_s,  customer[num_i].time_i );
    If you want to use tabs in in a printf statement, use \t.
    Also, a 257 character line is bad form. Split the command into multiple lines:
    Code:
    printf ("%d %s %s\t%s, %s, %s\t%s   %d\n", num_i + 1, customer[num_i].forename_s, 
                               customer[num_i].surname_s, customer[num_i].address1_s, 
                               customer[num_i].address2_s , customer[num_i].postcode_s,  
                               customer[num_i].telephone_s,  customer[num_i].time_i )
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Also 0 is an inapproriate value
    A 0 argument is treated as a null pointer, which is a valid argument that causes all output streams to be flushed.
    My best code is written with the delete key.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Originally posted by Prelude
    >Also 0 is an inapproriate value
    A 0 argument is treated as a null pointer, which is a valid argument that causes all output streams to be flushed.
    I didn't say invalid. At the point fflush(0); is used it's meant to clear the input buffer based on it's placement. It therefore is inappropriate IMO.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    66

    Thumbs up

    thanks everyone its working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  2. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM
  3. How to create and display a bitmap programatically?
    By solar3147 in forum Windows Programming
    Replies: 4
    Last Post: 06-24-2003, 02:55 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Replies: 0
    Last Post: 03-15-2002, 01:07 PM

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