Thread: Task 7.4, help

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    Post Task 7.4, help

    //Question =
    1. Declare a structure which contains minimum two elements
    2. in main function declair a local variable of the structure
    3. read in the values for the structure elements (use scanf)
    4. call a function print_data passing the local structure variable
    5. print the values of the structure in the print_data

    // code that ive been trying to get working all day :(

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct petdataa
    {
        int idtag;
        char name[20];
        float weight;
    };
    
    
    
    void print_data(int, char[], float);
    
    
    int main()
    {
        int i, nop = 2;
        struct petdataa petdata[2];
    
    
        for(i=0;i<nop;i++)
        {
            printf("Enter id-tag, name and weight of pet %d seperating each with a space:\n", i+1);
            scanf("%d%s%f", &petdata[i].idtag, &petdata[i].name, &petdata[i].weight);
        }
        for(i=0;i<nop;i++)
        {
            print_data(petdata[i].idtag, petdata[i].name, petdata[i].weight);
        }
    
    
    
    
    
    
        system("pause");
        return 0;
    }
    
    
    void print_data(struct petdataa display)
    {
        printf("/n%d%s%f/n",display.idtag, display.name, display.weight);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Franksy
    1. Declare a structure which contains minimum two elements
    Looks like you did that with struct petdataa.

    Quote Originally Posted by Franksy
    2. in main function declair a local variable of the structure
    You kinda did that, but not exactly. I would have expected:
    Code:
    struct petdataa petdata;
    rather than an array.

    Quote Originally Posted by Franksy
    3. read in the values for the structure elements (use scanf)
    It looks like you did that in main, but with a small mistake: &petdata[i].name should have been petdata[i].name because an array is converted to a pointer to its first element in this context.

    Quote Originally Posted by Franksy
    4. call a function print_data passing the local structure variable
    You're supposed to pass the "local structure variable", or more likely a pointer to that. I would expect:
    Code:
    void print_data(struct petdataa petdata);
    or more typically:
    Code:
    void print_data(const struct petdataa *petdata);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
     * 1. Declare a structure which contains minimum two elements
     * 2. in main function declair a local variable of the structure
     * 3. read in the values for the structure elements (use scanf)
     * 4. call a function print_data passing the local structure variable
     * 5. print the values of the structure in the print_data
    */
    
    struct petdataa
    {
        int idtag;
        char name[20];
        float weight;
    };
    
    //does not matter what you called your struct varitable name
    //in main. as you see in your function you can "rename" it.
    //just need to use the names in your prams now
    //to reference it.
    
    //it knows what to use because of your "data type"
    //using the struct you declared name.
    
    // size_t is for taking care of whatever data type that
    //system uses for the way it is being called.
    // long int etc...
    //
    void print_data(struct petdataa DATA[], size_t amout)
    {
        size_t count = 0;
        
        while (count < amout)
        {
            printf("Name %s ID %d Weight %.02f\n", DATA[count].name,DATA[count].idtag,
                                                    DATA[count].weight);
            count++;
        }
    }
    
    int main()
    {
            
        int count = 0, amount = 0;
        
        // bonus code
        
        printf("Enter amount of records you want to fill in\n");
        scanf(" %d", &amount);
        
        
        //struct petdataa petdata[2];
        // creates the array the size told at 
        //run time.
        
        struct petdataa petdata[amount];
    
        while ( count < amount )
        {
            printf("Enter a name, ID number and a weight in float notation.\n");
        
        
            scanf(" %19s %d %f", petdata[count].name, &petdata[count].idtag,
                                &petdata[count].weight);
            count++;
        }
        
        //pass your entire struct into your function, 
        //that is where your data values are at.
        //
        //you, the programmer already know what this
        //function is going to be used for, 
        //it is you that is writing it 
        //for your use in your program.
        //so use that to your advantage.
        
        print_data(petdata, sizeof(petdata)/sizeof(petdata[0]) );     
        
        return 0;
    }
    Last edited by userxbw; 12-16-2017 at 11:17 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do not use strcpy here: the original would have been correct once the address-of operator was removed, although the %s should have been changed to %19s to avoid buffer overflow. In fact, calling strcpy(petdata.name, name) leads to undefined behaviour because name was not initialised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I;ve always an into issues using direct assignment of a char array. let e go check that over and give it a try, ( again) scanf is not one of my favorite functions. before time runs out to fix it in here.

    Ok I stand corrected, now I am trying to remember where that issue was when I have it. Because that is the "fix" for that.ha

    and thanks for that reminder about size declarations in scanf..
    Last edited by userxbw; 12-16-2017 at 11:15 AM.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    thank you for your help guys : D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me do my task
    By mykelis in forum C Programming
    Replies: 2
    Last Post: 09-10-2017, 08:38 AM
  2. Help from the task
    By MAJA83 in forum C Programming
    Replies: 1
    Last Post: 06-06-2012, 11:35 AM
  3. Need help to do the big task
    By Steve Cao in forum Linux Programming
    Replies: 1
    Last Post: 07-16-2010, 02:01 PM
  4. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  5. Task Bar
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 07-22-2004, 07:37 AM

Tags for this Thread