Thread: promblem with sending struct through function

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    14

    promblem with sending struct through function

    So far i haven't started the calculation of the program but more worried about getting the first and last name to copy over to the output file w2.... even when i try to put it into a printf output of the first and last name... if anybody can try to walk me through what im doing wrong it would be much aprriceated
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_LEN 30
    #define SIZE 20
    struct employee {
           char* first[MAX_LEN];
           char* last [MAX_LEN];
           double payperhr;
           double taxes;
           double hours_in_week;
                    };              
                    
    void function(struct employee all);
    int main(){
       int i;
       int week, employee_day;
       double minin,hrin;
       double minout,hrout;
       struct employee all;
       int num_employee;
       FILE* fp;
    function(all); 
     
    system("pause");
    return 0;
    }
    void function(struct employee all){
         
       int i;
       int week, employee_day;
       double minin,hrin;
       double minout,hrout;
       int num_employee;
       FILE* ifp;
       FILE* ofp;
        
     ifp = fopen("clock.txt", "r");
     
     fscanf(ifp, "%d", &num_employee);
     
     for(i=0; i< num_employee; i++)
        {
              
     fscanf(ifp, "%s%s%lf", &all.first[i], &all.last[i], &all.payperhr);
        }
     //printf("%s%s", all.first[1], all.last[1]); 
        
     fscanf(ifp, "%d", &week);
     
     for(i=1; i<=week; i++)
        {
          fscanf(ifp, "%d", &employee_day);
          fscanf(ifp, "lf%lf%lf%lf", &minin, &hrin, &minout, &hrout); 
        }
        
     ofp = fopen("w2.txt", "w");
     
     fprintf(ofp, "Number of Employees: %d\n\n", num_employee);
     
     for(i=0; i<num_employee; i++)
         {
     fprintf(ofp, "W2 Form\n-------\n");
     
     fprintf(ofp, "Name: \n");
     fprintf(ofp, "Gross Pay: \n");
     
     fprintf(ofp, "Taxes Withheld: \n");
     
     fprintf(ofp, "Net Pay: \n");
     
     fprintf(ofp, "\n");
    }
    fclose(ifp);
    fclose(ofp);        
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You are lucky this didn't just end the universe.

    I don't know where to begin so I'm just going to tell you to pull back and start over on a smaller task so that you might refine this into something not insane.

    Let's start with string input and how you don't know how to use it.

    Can you write a simple example that asks a user for a name and prints that back to the screen?

    Assuming you can, do you spot the relevant difference between that new code and what you've done here? (Hint: Everything. But focus on `fscanf' and friends for now.)

    Soma

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    14
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    int main(){
        
        char state[30];
        
        printf("enter:");
        
        scanf("%s", state);
        
        printf("%s", state);
        
        system("pause");
        return 0;
        
    }
    so are you hinting at the &? when i have it runs smoothly but without it crashes.. only reason i honestly kept them there...other than that im not sure what ur hinting at.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Upon these lines differences lurk...

    char* first[MAX_LEN];
    char state[30];
    Soma

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    14
    * that lil guy?

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yes.

    In the correct code of the example you have allocated space for 30 `char' and told `fscanf' to store string input in that area.

    That isn't even similar to what you've done in your original code.

    Copy your code to a new files, so you have a backup, and then "gank out" everything that doesn't match your correct example (except for the names of things of course).

    Soma

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    14
    so bascially just keep the scan and everything needed for the scan of the char... correct?

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    14
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_LEN 30
    #define SIZE 20
    struct employee {
           char first[MAX_LEN];
           char last [MAX_LEN];
           double payperhr;
           double taxes;
           double hours_in_week;
                    };              
                    
    int main(){
       int i;
       int week, employee_day;
       double minin,hrin;
       double minout,hrout;
       struct employee all;
       int num_employee;
       FILE* ifp;
       FILE* ofp;
        
     ifp = fopen("clock.txt", "r");
     
     fscanf(ifp, "%d", &num_employee);
     
     for(i=0; i< num_employee; i++)
        {
              
     fscanf(ifp, "%s%s%lf", all.first[i], all.last[i], &all.payperhr);
        }
     //printf("%s%s", all.first[1], all.last[1]); 
        
     
    fclose(ifp);  
     
    system("pause");
    return 0;
    }
    put it in main to try to make it easier to deal with

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yes, that lil * guy!! That means 'declare first to be an array of MAX_LEN character pointers'.

    So whip that * out!

    A big problem is that you have a file with many employees, but only 1 "employee" struct. For now (I agree with phantomotap that you should start with something simpler) just try reading the first employee from the file into the struct and printing first and last name

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I'm only trying to get you to see what is conceptually different between what you know is correct and what you mistakenly wrote.

    If you've done that without breaking things back down feel free to ignore those instructions.

    If you haven't done that removing some of the details that aren't strictly relevant will help you focus on what is important.

    In the future, start small and add functionality after proving code correct. I can safely assume that you basically know what to do but didn't set aside enough time to reason about how to go about it. Even after ~25 years of coding experience I still follow the advice I was given a long time back: "Compile often; test often.".

    My point here is, once you chased your problem down make it a point to learn not just the solution but how you discovered the solution. If you hurriedly add a lot of code back once you have a solution you will probably introduce a lot more bugs. Instead, take your time to add a few lines and then test those few lines.

    *smashes cockroach*

    I command thee Salem, post that beginner development process tutorial you've been linking lately.

    O_o

    Why do I have cockroaches?

    Soma

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    put it in main to try to make it easier to deal with
    Now you should start getting a handle on your problem.

    As smokeyangel said, get the code to read one employee first. You can add a loop to read more employees later after you can read one properly. You very nearly have that done. The problem, as for now, is that you haven't completely matched known working code to what you have because you are trying to read multiple employees into what is conceptually a single entity.

    Soma

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    14
    ok ill try to make things as simple as possible and go through notes... thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP- Sending and Receiving a Struct
    By rishanth12 in forum C Programming
    Replies: 3
    Last Post: 09-12-2011, 12:32 AM
  2. Sending Struct pointers through pipes 2
    By kiros88 in forum C Programming
    Replies: 1
    Last Post: 09-02-2009, 05:52 PM
  3. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  4. sending a struct to a function
    By salvadoravi in forum C Programming
    Replies: 15
    Last Post: 12-21-2007, 07:08 AM
  5. Promblem with the .h
    By roca45 in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 12:38 AM

Tags for this Thread