Thread: how do I populate a structure containing a ptr with user input?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    Question how do I populate a structure containing a ptr with user input?

    The following code sets up an array of structures that is partially populated. one of the members is a pointer to a string (lunch[i].name). How do I populate the rest of the structure by user input? A 'for loop' causes a runtime error.


    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;

    struct food
    {
    char *name;
    int weight, calories;
    };
    const int MAXITEMS = 5;
    void printList(food *lunch);

    int main()
    {
    food lunch[MAXITEMS] = // see note 9.3
    {
    {"apple", 4, 100},
    {"salad", 2, 80},
    };

    lunch[2].name = "onion";
    lunch[2].weight = 1;
    lunch[2].calories = 40; // This works: compiles and printList prints it.



    printList(lunch);

    return 0;
    }

    void printList(food *lunch)
    {
    cout << " Food Name Ounces Cal" << endl;
    cout << " --------- ------ ---" << endl;
    for(int j = 0; j < 3/*MAXITEMS*/; j++)
    cout << setw(15) << lunch[j].name << " "
    << setw(6) << lunch[j].weight << " "
    << setw(5) << lunch[j].calories << endl;

    return;
    }
    rc7j

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need to allocate space
    Code:
    char temp[100];
    cin >> temp;  // or cin.getline(temp,100);
    
    // now allocate space and copy
    lunch[i].name = new char[strlen(temp)+1];
    strcpy( lunch[i].name, temp );
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    thanks. that works. Now how do I allocate space without specifying the length of the string - like making a ragged array? ref. the '*name' pointer in the struct definition.
    Last edited by rc7j; 11-05-2001 at 05:17 PM.
    rc7j

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    using an array of c_style strings I don't think you can. You could with a list of your own design or from STL. STL would also allow you to create a vector of strings (STL strings, not c_style strings), which is the other way I can think of doing this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  4. 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
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM