Thread: Having a problem with a struct.

  1. #1
    C Newbie
    Join Date
    Oct 2011
    Posts
    59

    Having a problem with a struct.

    Source
    Code:
    #include <stdio.h>
    
    
    int fill_array(void);
    int display_array(int count);
    
    
    typedef struct
    {
        char worker_name[20];
        float  hourly_pay;
        int  hours;
    }WorkerRecord;
    
    
    int main()
    {
        WorkerRecord info[50];
        int count, k;
        float average_wage;
        
        count = fill_array();
        display_array(count);
        
        for(k = 0, average_wage = 0; k < count; k++)
        {
            average_wage += info[k].hourly_pay;
        }
        
        average_wage /= count;
        
        printf("\nAverage Hourly Wage: $%.2f", average_wage);
    
    
        printf("\n\nPress any key to continue...");
        getchar();
        return 0;
    }
    
    
    int fill_array(void)
    {
        WorkerRecord info[50];
        int count = 0;
        
        FILE * pointer;
        
        pointer = fopen("workers.txt", "r");
        
        if(pointer == NULL)
        {
            printf("The file can not be found.\n\n\n");
            printf("Press any key to continue...");
            getch();
            return 1;
        }
        
        while(!feof(pointer))
        {
            fscanf(pointer, "%s%d%f", info[count].worker_name, &info[count].hours, &info[count].hourly_pay);
            count++;
        }
    
    
        fclose(pointer);
        
        return (count);
    }
    
    
    int display_array(int count)
    {
        WorkerRecord info[50];
        int k;
        float gross_pay[50];
        
        
        printf(" Name\t\tHours\t\tHourly Pay\tGross Pay\n");
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        
        for(k = 0; k < count; k++)
        {
            gross_pay[k] = (info[k].hourly_pay * info[k].hours);
        }
        
        for(k = 0; k < count; k++)
        {
            printf("%s\t\t%d\t\t$%.2f\t\t$%.2f\n", info[k].worker_name, info[k].hours, info[k].hourly_pay, gross_pay[k]);
        }
        
        return 0;
    }
    Workers.txt
    Code:
    Bob    40    10
    Sean    35    12
    Linda    30    15
    Michael    45    9
    Kelly    25    11
    The area I am having trouble with, is in the main function when I am trying to compute/print the average hourly wage. For some reason, I am not getting the values from the struct correctly, even when i had no problems with my other two functions.

    Thanks in advance,
    Alan
    Last edited by Alan Gott; 12-04-2011 at 02:05 PM.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    The problem is that in the function display_array(int count) you create a new WorkerRecord struct. This means that the values in this variable have not been set and can cause undefined behavior. you need to be able to pass by reference the WorkerRecord array to the fill_array function update the values and than pass this to display_array to display the values. That is at least one way, if you cannot change the function prototypes than you probably will have to create a golbal varaible outside of main and declare it static than use them within both functions to do what you need.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also take a real close look at line 27 ... two problems.

  4. #4
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by Durango2011 View Post
    The problem is that in the function display_array(int count) you create a new WorkerRecord struct. This means that the values in this variable have not been set and can cause undefined behavior. you need to be able to pass by reference the WorkerRecord array to the fill_array function update the values and than pass this to display_array to display the values. That is at least one way, if you cannot change the function prototypes than you probably will have to create a golbal varaible outside of main and declare it static than use them within both functions to do what you need.
    I know what a global varaible is, but I am not sure what you mean by declaring it static.
    Quote Originally Posted by CommonTater View Post
    Also take a real close look at line 27 ... two problems.
    What's wrong with that? Doesn't it add the next variable in the array to average _wage in each loop?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    I know what a global varaible is, but I am not sure what you mean by declaring it static.

    What's wrong with that? Doesn't it add the next variable in the array to average _wage in each loop?
    You are using average_wage without initializeing it's value.

    Also for your functions... remove the struct definition and pass in a pointer to the array in main.

  6. #6
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    You are using average_wage without initializeing it's value.
    I noticed, and changed, that in line 25 didn't I? (It might have been after you read it and before you posted this).

    Quote Originally Posted by CommonTater View Post
    Also for your functions... remove the struct definition and pass in a pointer to the array in main.
    I am not sure what you mean here, I still only have limited knowledge on nearly everything more complex than the basics, which was what my class covers.
    ~~~~~
    EDIT: Also, I am having a problem when I transfer my code from Geany to MSVC++.

    <http://imgur.com/a/u5kRj>

    The one that isn't full of random characters is from Geany.
    Last edited by Alan Gott; 12-04-2011 at 03:03 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    I noticed, and changed, that in line 25 didn't I? (It might have been after you read it and before you posted this).
    Get it out of the for() statement. Really... bad idea!

    Put it on the line before if you want it outside the loop.
    If you want it reset to 0 every time through, put it inside the loop.

    Don't put it in the for statement!

    I am not sure what you mean here, I still only have limited knowledge on nearly everything more complex than the basics, which was what my class covers.
    ~~~~~
    EDIT: Also, I am having a problem when I transfer my code from Geany to MSVC++.

    <Photo Album - Imgur>

    The one that isn't full of random characters is from Geany.
    Then you need to do some serious reading on functions and pointers... Essentially, what happens inside a function stays inside the function and is destroyed when the function returns...

  8. #8
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    Then you need to do some serious reading on functions and pointers... Essentially, what happens inside a function stays inside the function and is destroyed when the function returns...
    Ok, well I knew that much then, I just didn't know the technical way of saying it. I guess my problem is not understanding how structs fit into that. Why is it that I can use fill_array to store data in the struct, and display_array to display it, but can't use it in my main function? I did the exact same thing in my main function as I did my display_array function, so they should have the same effect, right?
    Last edited by Alan Gott; 12-04-2011 at 09:15 PM.

  9. #9
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    BUMP? I feel as though I am so close, yet so far from finally understanding why this isn't working.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The way you are doing this, you are working from 3 completely separate arrays...

    info[50] in fillarray() is not the same array as info[50] in main ... they are two separate entities.
    The one in fillarray() is destroyed when the function returns.

    You have to pass a pointer to the array into your function from main in order to access the array in main...
    You cannot declare a new array in your function because that hides the one in main.

  11. #11
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    The way you are doing this, you are working from 3 completely separate arrays...

    info[50] in fillarray() is not the same array as info[50] in main ... they are two separate entities.
    The one in fillarray() is destroyed when the function returns.

    You have to pass a pointer to the array into your function from main in order to access the array in main...
    You cannot declare a new array in your function because that hides the one in main.
    How was I able to access it in display_array then, and how am I able to return two things, as I have never done that as of yet.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And what did it print out for you?

    You are getting the wrong values because it's working from random junk in the copy of info[50] in main.
    C does not initialize arrays to 0... whatever random values are in memory where an array is created will be what you get.

    Try it like this...
    Code:
    #include <stdio.h>
    
    
    int fill_array(void);
    int display_array(int count);
    
    
    typedef struct
    {
        char worker_name[20];
        float  hourly_pay;
        int  hours;
    }WorkerRecord;
    
    
    int main()
    {
        WorkerRecord info[50];
        int count, k;
        float average_wage;
        
        count = fill_array(info, 50);
        display_array(info, count);
        
        for(k = 0, average_wage = 0; k < count; k++)
        {
            average_wage += info[k].hourly_pay;
        }
        
        average_wage /= count;
        
        printf("\nAverage Hourly Wage: $%.2f", average_wage);
    
    
        printf("\n\nPress any key to continue...");
        getchar();
        return 0;
    }
    
    
    int fill_array(int *arr, int max)
    {
        int count = 0;
        
        FILE * pointer;
        
        pointer = fopen("workers.txt", "r");
        
        if(pointer == NULL)
        {
            printf("The file can not be found.\n\n\n");
            printf("Press any key to continue...");
            getch();
            exit(1);
        }
        
        while {fscanf(pointer, "%s%d%f", arr[count]->worker_name, &arr[count]->hours, &arr[count]->hourly_pay) > 2)   
           {
            count++;
            if (count >= max)
              break;
           }
        fclose(pointer);
        
        return (count);
    }
    
    
    int display_array(int *arr, int count)
    {
         int k;
         float grosspay[50];    
        
        printf(" Name\t\tHours\t\tHourly Pay\tGross Pay\n");
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        
        for(k = 0; k < count; k++)
        {
            gross_pay[k] = (arr[k]->hourly_pay * arr[k]->hours);
        }
        
        for(k = 0; k < count; k++)
        {
            printf("%s\t\t%d\t\t$%.2f\t\t$%.2f\n", arr[k]->worker_name, arr[k]->hours, arr[k]->hourly_pay, gross_pay[k]);
        }
        
        return 0;
    }
    Last edited by CommonTater; 12-04-2011 at 11:43 PM.

  13. #13
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    And what did it print out for you?

    You are getting the wrong values because it's working from random junk in the copy of info[50] in main.
    C does not initialize arrays to 0... whatever random values are in memory where an array is created will be what you get.
    In display_array, everything got printed correctly (see the first image in this link <Photo Album - Imgur>), which is why I am having trouble understanding what I need to do differently.

    EDIT:
    Code:
    gross_pay[k] = (arr[k]->hourly_pay * arr[k]->hours);
    Now I am severely confused. Not only am I not understanding where arr is coming from, but I've never seen "->" used in code as of yet. And furthermore, will that change anything in my main function? The only problem I was having from the getgo was computing the average hourly wage in the main function. But, when you mentioned that I kept making new arrays in each function, I was wondering why my display_array function worked even though my only input was the count.
    Last edited by Alan Gott; 12-04-2011 at 11:09 PM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    In display_array, everything got printed correctly (see the first image in this link <Photo Album - Imgur>), which is why I am having trouble understanding what I need to do differently.
    What compiler were you using with Genie?
    That it worked there is probably just pure dumb luck.

    That it doesn't work in msvc is totally expected.

  15. #15
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    What compiler were you using with Genie?
    That it worked there is probably just pure dumb luck.

    That it doesn't work in msvc is totally expected.
    I'm not too keen on a lot of the technical stuff when it comes to building and compiling, but I'd have to say gcc just from a quick look.

    imgur: the simple image sharer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct struct struct problem....please....help!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  2. Struct problem... Can anyone help?
    By nullifyed in forum C Programming
    Replies: 9
    Last Post: 05-21-2010, 04:48 PM
  3. problem with struct?
    By cangel in forum C Programming
    Replies: 8
    Last Post: 09-27-2008, 11:35 PM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Problem about struct
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-19-2001, 12:15 PM