Thread: creating a struct based on user input

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

    creating a struct based on user input

    ive never done many structs ,if my program asks the user to enter a name for a new "member", how would i make
    a member with the string they entered?
    Code:
    struct rec
    {
    int stuff[6];
    char blah;
    float water;
    };
    char newm[20];
    printf("enter name for a new member to add to struct: ");
    fgets(newm, 20,stdin);
    struct rec newm;   /* i know this wont work for sure, it will create a member named "newm",but how would i make it make a member with the input the user entered? */
    theres a idea of what i want to do, but OF COURSE doesnt work

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    You cant create a variable name from input.

    But you can make a feild named 'newm' in struct rec. and put the name of the new member there. You can use an array to add a list of members.


    Code:
    struct rec
    {
     char newm[20];   // New Member's Name
    int stuff[6];
    char blah;
    float water;
    };
    struct rec newm[100];
    char newm[20];
    printf("enter name for a new member to add to struct: ");
    fgets(newm, 20,stdin);
     strcpy( newm[indexhere].newm, newm)   // copy newm to new member

    And you can use a list which requires memory allocation. http://www.cprogramming.com/tutorial/lesson15.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM