Well here we go again

I actually thought I was going to get this one on the first shot. The project calls for me to use to input files and create an output file. I have a name, id #, and payrate. And then I have just the id# and hours on the other file. I realized I have to use an if statement to compare the two id#'s so I can calculate the payrate. Also through google I know I have to use seekg() to search for matches. I really don't understand why it's not working but here is my code and the errors i'm receiving. I just want to get pointed in the right direction as to what might be wrong because what the error is showing me is greek to me right now. Hopefully my organization is better and therefore easier to read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

#define in_emp "employees.txt"          //defining local file employees.txt
#define in_hrs "hours.txt"              //defining local file employees.txt
#define out_pay "payroll.txt"           //output file for payroll

//Prototype
//Processes employees.txt and hours.txt then writes info to payroll.txt

void process_ifiles(ifstream, ifstream, ofstream);

int main()
{
    ifstream nemp;              //nemp is input stream 
    ifstream nhrs;              //nhrs is input stream
    ofstream opay;              //opay is output stream
    
    //Open input files and output file
    
    nemp.open(in_emp);          //connects nemp to employees.txt
    nhrs.open(in_hrs);          //connects nhrs to hours.txt
    
    //if neither ifstream open
    if (nemp.fail() || nhrs.fail())
    {
        cerr << "*** Cannot Open "<<"for input." << endl;
        return EXIT_FAILURE;
    }
    
    opay.open(out_pay);         //connects opay to payroll.txt
    
    //if opay doesn't open
    if (opay.fail())
    {
    cerr << "*** Cannot Open " << in_emp << " for output." << endl;
        return EXIT_FAILURE;
    }
    
    process_ifiles(nemp, nhrs, opay); // run function process_ifiles
    
    return 0;
}

//function definition

void process_ifiles(ifstream nemp, ifstream nhrs, ofstream opay)
{
    string name;        //stores employee name
    int id_num;         //stores employee id#
    float pay_rate;     //stores employee pay rate
    float tot_hrs;      //stores employee total hours worked (if any)
    float payamt;       //pay_rate * tot_hrs for pay
    float id_num2;
    
    while (!nemp.eof()) //enter loop for employees.txt
    {
        nemp >> name >> id_num >> pay_rate;     //enters info n2 variables
        
        
        
        while (!nhrs.eof())
        {
            nhrs >> id_num2 >> tot_hrs;
        }
        
        nhrs.clear();                      //clears seek through nhrs
        nhrs.seekg (0, ios::beg);          //seeks 
        
        if(id_num = id_num2)
        {
            payamt = tot_hrs * pay_rate;
            
            opay << name << " " << id_num << " "<<pay_rate<<" "<<tot_hrs<<
                    "Total Pay for this Period: " << payamt<<endl;
        }
        else
            opay << name << " " << id_num << " "<<pay_rate
                    <<"No hours this Period "<< "Total Pay: $0" <<endl;
    
    }
        

    return 0;
}


Here is the error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/d_compsc
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/chpt8.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/chpt8.o.d -o build/Debug/GNU-MacOSX/chpt8.o chpt8.cpp
/usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/include/c++/4.2.1/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
/usr/include/c++/4.2.1/iosfwd:55: error: within this context
/usr/include/c++/4.2.1/iosfwd: In copy constructor 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)':
/usr/include/c++/4.2.1/iosfwd:89: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here 
/usr/include/c++/4.2.1/streambuf: In copy constructor 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)':
/usr/include/c++/4.2.1/streambuf:794: error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/include/c++/4.2.1/iosfwd:86: error: within this context
/usr/include/c++/4.2.1/iosfwd: In copy constructor 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)':
/usr/include/c++/4.2.1/iosfwd:89: note: synthesized method 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)' first required here 
chpt8.cpp: In function 'int main()':
chpt8.cpp:48: note: synthesized method 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)' first required here 
chpt8.cpp:48: error:   initializing argument 1 of 'void process_ifiles(std::ifstream, std::ifstream, std::ofstream)'
chpt8.cpp: In function 'void process_ifiles(std::ifstream, std::ifstream, std::ofstream)':
chpt8.cpp:92: error: return-statement with a value, in function returning 'void'
make[2]: *** [build/Debug/GNU-MacOSX/chpt8.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 868ms)
Your problem is that you are not able to pass ofstream/ifstream objects by value as it tries to call the copy constructor which in this case is private. As kind of shown here.
/usr/include/c++/4.2.1/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private

Change it to passing them by reference instead.
void process_ifiles(ifstream &nemp, ifstream& nhrs, ofstream &opay)
(Don't forget to change the prototype as well)

On another note your process_ifiles function is defined as void, however is returning 0.
Topic archived. No new replies allowed.