Thread: problem with structures

  1. #1
    Unregistered
    Guest

    problem with structures

    I have a problem when i use a structure in a function....

    struct task
    {
    char task_descrip[50];
    cont_info contractors;
    material_info materials;
    };

    is invoked in :

    void add_task(task& obj){
    char ch;
    cout<<"\n\n Please Enter the Following Information:";
    for(int i=0; i<100; i++){
    cout<<"\n\n Enter a brief task description: ";

    cin.get(obj[i].task_descrip);
    cin.ignore(100,'\n');

    which is where i get an error, saying...

    In function `void add_task(task &)':
    : no match for `task &[int &]'

    what am i doing wrong here?

    Please help.....

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    how is add_task being called? From this statement:

    >cin.get(obj[i].task_descrip);

    it looks like obj is an array, but you dont declare it as such in your function definition:

    >void add_task(task& obj)

    you declare obj as a reference to task, not as an array. Maybe you meant

    add_task(task *obj)

  3. #3
    Unregistered
    Guest
    the argument to add_task() is a reference of type task called obj. As such obj will have a single example of each of the struct member variables, one of which is a char array called task_description. This char array can be viewed as a string. the best approach to place a string in this variable will be with getline() rather than get():

    cin.getline(obj.task_descrip, 49);

    which allows you to drop the char by char data entry associated with the loop. getline() allows inclusion of whitespace, such as spaces, in the string.

    The call to ignore() is probably best used before the call to getline(), especially if you have used a call to >> at any time before the call to getline().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM