Quick way of filling structure?

This is a discussion on Quick way of filling structure? within the C Programming forums, part of the General Programming Boards category; Does anyone have any ideas of how I can make this job easy for myself. As you can see, this ...

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

    Quick way of filling structure?

    Does anyone have any ideas of how I can make this job easy for myself. As you can see, this array of structures contains 100. It will take me a long time to type every detail in manually, Im sure theres a easier way round this. I thought of using a for statement to count me all the way through promting me for the deatails then puting them in the relevant place, can anyone give me an example of this idea (if it is the best way).

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    
    
    
    
    struct database {
      char prod[50];
      float cal;
      float carb;
      float prot;
      float fat;  
    };
    
    
    int main()
    {
      struct database mainm[100];  
                                 
      strcpy(mainm[1].prod, "Chips tesco steak cut oven");
      mainm[1].cal = 141.0;
      mainm[1].carb = 24.4;
      mainm[1].prot = 2.0;
      mainm[1].fat = 3.9;
      
      strcpy(mainm[1].prod, "Chips asda steakcut frying");
      mainm[2].cal = 187.0;
      mainm[2].carb = 28.0;
      mainm[2].prot = 2.9;
      mainm[2].fat = 7.0;
      
    
      
      getch();
      
      
    }
    Last edited by voodoo3182; 08-04-2005 at 08:05 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    struct database {
      char prod[50];
      float cal;
      float carb;
      float prot;
      float fat;  
    };
    
    struct database d = {"Chips tesco steak cut oven", 141.0, 24.4, 2.0, 3.9};
    [edit]
    Or, alternatively,
    Code:
    struct database d = {
        .prod = "Chips tesco steak cut oven",
        .cal = 141.0,
        .carb = 24.4,
        .prot = 2.0,
        .fat = 3.9
    };
    [/edit]
    Last edited by dwks; 08-04-2005 at 08:58 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.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by voodoo3182
    Does anyone have any ideas of how I can make this job easy for myself. As you can see, this array of structures contains 100. It will take me a long time to type every detail in manually, Im sure theres a easier way round this.
    Parse a text file containing the information?
    Deal with it and hard-code an initialization?

    Arrays are zero-based.
    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.*

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > .prod = "Chips tesco steak cut oven",
    You need a C99 compiler to support this notation.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help filling a structure with data from a file.
    By System_159 in forum C++ Programming
    Replies: 41
    Last Post: 09-27-2006, 06:39 PM
  2. Filling a structure
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 12-05-2005, 03:00 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:05 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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