Thread: help displaying info from structure

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    help displaying info from structure

    ok so the idea behind this is i have a program where info is entered for an account number, a last name, and a balance and it is stored in a structure.

    afterwards the info from the structure is suppose to be displayed in a formatted manner (as you can see in the code below), however i am only getting garbage numbers on the output. The account number is coming out random, the last name does not show at all, and the balance is coming out all zeroes.

    Can anyone give me a pointer and what could be causing this? I made a program similar to this one a bit ago and it worked fine, and i am going mostly off that format, so im a bit lost on what i am missing here.

    Any help is appreciated!

    Code:
    #include <stdio.h>
    
    /* Define structure */
    struct info
    {
    	long int accntNum;
    	char name[25];
    	float balance;
    };
    
    /* Begin main */
    main()
    {
    	/* Declare variables */
    	struct info client[25];
    	int x, numClient;
    	
    	/* Display opening message */
    	printf ("Welcome to Client Account Information Services\n\n");
    	
    	/* Prompt for how many clients will have info entered */
    	printf ("Enter number of clients to be used: ");
    	scanf ("%i", &numClient);
    	
    	/* Prompt for user to enter each clients info */
    	for (x = 0; x < numClient; x++)
    	{
    		printf ("\nEnter account number: ", x + 1);
    		scanf ("%ld", &client[x].accntNum);
    		
    		fflush(stdin); /* Remove extraneous characters */
    		printf ("Enter last name: ", x + 1);
    		gets (client[x].name);
    		fflush(stdin); /* Remove extraneous characters */
    		
    		printf ("Enter balance: ", x + 1);
    		scanf ("%f", &client[x].balance);
    	} /* end for */
    	
    	/* Display each clients information */
    	printf ("\nACCOUNT    LAST NAME                          BALANCE\n");
    	for (x = 0; x < numClient; x++);
    	{
    		printf ("\n%.4ld    %s                          %.2f", client[x].accntNum, client[x].name, client[x].balance);
    	} /* end for */
    	
    	return 0;
    	
    } /* end main */
    and this is a sample of what my output looks like no matter how many clients i attempt to enter:
    Code:
    Welcome to Client Account Information Services
    
    Enter number of clients to be used: 1
    
    Enter account number: 100
    Enter last name: smith
    Enter balance: 300
    
    ACCOUNT    LAST NAME                          BALANCE
    
    1245512                              0.00Press any key to continue . . .

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As has been discusse MANY times today, fflush(stdin) is officially undefined. It may do any of the following (and just about anything else):
    1. Work "as you expect it".
    2. Do nothing.
    3. Crash your application.
    4. Something random, e.g. outputting rubbish to the screen.
    5. Launch atomic weapons.

    Since it may well do the 4 other options than 1, it's best to use the method described in the FAQ "How do I empty the input buffer" (or something like that).

    Your problem may actually be caused by this: Perhaps it's doing #2 on the list above.

    You also should not use gets(), as it's a very good way to cause your program to crash when too much input is given.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    even when i take out the fflush(stdin) statements the program outputs the same rubbish

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Do you see anything abnormal about the body of the second for loop?

    Edit: or perhaps I should say that the for loop has a NULL body
    Last edited by itCbitC; 04-04-2009 at 12:48 PM.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    yep, just noticed it. wow, thanks semi-colons.

    i must have been on auto-pilot when i put that one in there.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    thanks for pointing out my dumb mistake!

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    as noted before fflush() is meant only for output streams

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  2. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  3. Function to return pointer to structure
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 06:10 AM
  4. nested structure help
    By whistler in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 10:48 AM
  5. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 12:35 PM