Thread: Reading records from File

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    Reading records from File

    Can someone help me to figure his out. I have to read in a file that contains employee names, numbers, payrates, and hours. Then I have to calculate the gross andsend everything to an output file. I keep getting this error message

    "employee.cpp" 69 lines, 1471 characters
    $ c++ employee.cpp
    Undefined first referenced
    symbol in file
    putdata(char *, int, float) /var/tmp/cckpUsol.o
    getdata(char *, int, float, int) /var/tmp/cckpUsol.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    $

    this is my program

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    const int MAXNAME = 20;
    const int NUMRECS = 12;
    int i=0;
    
    struct employees
    {
    int number, hours;
    float rate, gross;
    char name[MAXNAME];
    }employee;
    
    int employees[NUMRECS];
    
    void getdata(char [], int, float, int);
    void processdata(float, int);
    void putdata(char [], int, float);
    
    ifstream HopeData;
    ofstream MikeData;
    
    void main()
    {
    
    
    HopeData.open("employee.dat");
    MikeData.open("putdata.dat");
    
    if(HopeData.fail())
    {
    	cout << "\n\nFile not successfully opened\n\n";
    }
    cout << "\n\nFile successfully opened\n\n";
    
    getdata(employee.name, employee.number, employee.rate, employee.hours);
    
    putdata(employee.name, employee.number, employee.gross); 
    
    HopeData.close();
    MikeData.close();
    }
    
    void getdata(int name[], int number, float rate, int hours)
    {
    		while(i < NUMRECS)
    		{
    
    HopeData >> employee.name >> employee.number >> employee.rate >> employee.hours;
    		}
    processdata(employee.rate, employee.hours);
    }
    
    void processdata(float rate, int hours)
    {
    employee.gross = employee.rate * employee.hours;
    }
    
    void putdata(char name, int number, float gross)
    {
    cout << "\n\n              Payroll  Report" << endl;
    cout << "        Name        Number        Gross Pay" << endl;    
    		cout << setiosflags(ios::showpoint);
    		cout << setiosflags(ios::fixed);
    		cout << setprecision(2);
    		cout << setw(15) << employee.name << setw(5) << "\t" << employee.number << setw(7) << "\t$" << employee.gross << endl;
    
    }
    And this is a sample of my file that i'm reading

    Simmons 2242 8.00 40
    Alexander 8343 5.98 40
    Jenkins 6234 4.65 23
    Gardiner 1009 9.34 40
    Ward 2289 3.57 40
    Simien 5342 3.09 40
    Smith 7344 7.00 40
    Fresch 2942 6.50 40
    Donato 5034 8.09 40
    Glasper 1276 8.32 40
    Marsh 8234 7.98 40
    Fontenot 2454 6.95 40
    Johnson 1523 6.98 40
    Deville 2212 1.45 40
    Clay 1912 2.89 40

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    main()'s return type is int, not void

    Please indent your code to make it easier to read.

    And don't use global variables. You should pass the file streams to and from the functions that need them.

    void putdata(char *name, int number, float gross)
    You forgot an asterisk there. I suggest using copy and paste instead of retyping to ensure that your function definitions match their declarations.
    Last edited by joshdick; 04-20-2005 at 08:00 AM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    thanx for the help but i dont know anything about passing fstreams I bearly know how to pass perameters I'm use to using global and local variables

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    That's precisely the problem. You shouldn't ever get used to using global variables. You can pass a filestream easily as a reference parameter.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    There's another descrepancy between function declaration and function definition in the two lines below.

    void getdata(char [], int, float, int);

    void getdata(int name[], int number, float rate, int hours)


    Fix the declaration/definition descrepancies and the problem will probably go away, whether you use global variables or not---though I agree that use of global variables should be minimized--when you have been taught how to do it.
    You're only born perfect.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You have both a struct employees and an int array named employees. Potential for confusion exists because of this. Change the name of one of them, or better yet why even need the int array? It seems you would want to make an array of structs instead.

    Code:
    struct employees
    {
        ...
    } employee[NUMRECS];  // Now employee is an array of NUMRECS structs
    All of your existing input code is simply reading into a single struct and overwritting the previous contents. Now that you have the above array of structs, your input code should read each new record into its own array index/slot. Your output function would then go through this array index-by-index an output the data for each slot.

    Passing file streams by reference is easy, as an example:

    Code:
    // Function prototypes
    void readfunc( ifstream&, // remaining arguments );
    void writefunc( ofstream&, // remaining arguments );
    
    int main()
    {
        // File Stream declarations
        ifstream input("input.txt");
        ofstream output("output.txt");
    
        // Function call
        readfunc(input, // remaining arguments );
        writefunc(output, // remaining arguments );
    
        return 0;
    }
    
    void readfunc( ifstream& infile, // remaining arguments )
    {
        // read using "infile"
    }
    
    void writefunc( ofstream& outfile, // remaining arguments )
    {
        // write using "outfile"
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM