filling an array of structures?

This is a discussion on filling an array of structures? within the C Programming forums, part of the General Programming Boards category; I have an array of structures which need filling by the user. I have tried doing this by declaring a ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    filling an array of structures?

    I have an array of structures which need filling by the user. I have tried doing this by declaring a pointer to the structure array, and then using the for loop to run the pointer through the array. Finally the structure will be saved as a bin to disk for later retrieval. Can someone please tell me what Im doing wrong as this just keeps skipping the second prod. Is this actually saving data to the structure? Am I going about this the wrong way all together. Ive been going through my "c for linux programming" book but cant find what to do? Any suggestions please. Here is the code.(Ive only posted the relevant parts, and the for loop stops at 37, just an example)



    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #include<string.h>
    
    
    
    
    /*structure to hold all info, final program will contain structures of main meal, snacks*/
    /*and also deserts, using this structure*/
    
    struct database {
      char prod[60];
      float cal;
      float carb;
      float prot;
      float fat;  
    };
    
    /*function prototypes*/
    
    int full_screen(void);
    void main_menu(void);
    void about_opt(void);
    void help_opt(void);
    
    
    
    
    FILE *fp;
    
    struct database *point1;
    
    int count;
    
    
    int main()
    {
        
    full_screen(); /*Puts the dos console into full screen when program is executed*/
    
    
      
        
      struct database mainm[100]; 
      
      point1 =&mainm[0];
       
      
                               
      
      
      for ( count = 0; count < 37; count ++ )
      {
          printf("Enter the product:");
          fgets(point1->prod, 60, stdin);
          printf("\nEnter cal:");
          scanf("%f", & point1->cal);
          printf("\nEnter carb:");
          scanf("%f", &point1->carb);
          printf("\nEnter protein:");
          scanf("%f", &point1->prot);
          printf("Enter fat:");
          scanf("%f", &point1->fat);
          }
    Last edited by voodoo3182; 08-06-2005 at 03:25 PM. Reason: spelling mistakes

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Why complicate things with a pointer?

    fgets(point1->prod, 60, stdin);
    would be
    fgets(mainm[count].prod, 60, stdin);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Ok salem, I got rid of the pointer, but why is it skipping the second .prod and going straight to the .cal

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    I think its something to do with fgets, any suggestions anyone?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually it has something to do with scanf. There's a FAQ on it. It gets asked nearly every day. scanf leaves the newline in, since you aren't providing any way to remove it, and it gets read for the input of the next scanf call.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Thankyou quzah, but would be grateful if you could show me what to do. Ive been to the FAQ and tried an example but to no avail. I know its just a simple thing, but for me its a big problem (Ive been working on this small program now for 4 days) as im just learning.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    I figured I can stop the loop with getchar or getch, and wait for user input, but it still doesnt allow me to enter a string, because it just jumps straight to the next line??

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you inisit on using scanf, then you'll need to get rid of the extra crap in the input buffer that is left behind. After each scanf call, add a call to something like:
    Code:
    void fflushstdin( void )
    {
        int c;
        while( (c=fgetc( stdin )) != EOF && c != '\n' );
    }
    This will pull out any pending input. Now this may not be what you want all the time, but in that case, you'll want to go read the FAQ on reading input and adjust your behavior.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Thankyou for your help, it is putting me on the right track every time

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    That function is very helpful, and I could use it a lot in my program. It works fine for me. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 02:58 PM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 02:39 PM

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