Please help if you can!

Pages: 12
First, I would like to introduce myself. I am an absolute Beginner in C++, and I am taking an Online Intro Course to learn it. I have a background in Computer Animation, as well as IT, so I wanted to learn some Programming. I have been lurking here since I started the Course and I have been amazed by some of what I have read. So when I ran into this proverbial brick wall, I figured this would be the place to help me find an answer.

Ok, so I am at a loss here. I am stuck on building a type of payroll program and I am hoping someone could point me in the right direction. Basically, what I need the program to do is have the User input his/her Employee ID, and then the code would call upon the "employee.txt" file to output the information. Here is what I have so far:

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
#include <iomanip>
#include <iostream>
#include <C:\...\payroll3\payroll3B\employee.txt>

using namespace std;

ifstream fin.open ("employee.txt");

main() {
int employeeid;
    float hoursworked, hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.30;
    cout<<"PLEASE ENTER THE EMPLOYEE NUMBER:";
    cin>>employeeid;
    grosspay = hoursworked * hourlyrate;
    taxamount = grosspay * TAXRATE;
    netpay = grosspay - taxamount;
while(fin>>employeeid>>hoursworked>>hourlyrate){
    cout<<"YOUR EMPLOYEE ID: "<<setw( 6 )<<setprecision( 4 )<<employeeid<<endl;
    cout<<"YOUR HOURS WORKED: "<<setw( 5 )<<setprecision( 4 )<<hoursworked<<endl;
    cout<<"YOUR HOURLY RATE: "<<setw( 6 )<<setprecision( 4 )<<hourlyrate<<endl;
    cout<<"YOUR GROSS PAY: "<<setw( 8 )<<setprecision( 4 )<<grosspay<<endl;
    cout<<"YOUR TAX AMOUNT: "<<setw( 7 )<<setprecision( 4 )<<taxamount<<endl;
    cout<<"YOUR NET PAY: "<<setw( 10 )<<setprecision( 4 )<<netpay<<endl;
    }//WHILE
    fin.close (""employee.txt");
    system ( "pause" );
    return 0;
} //MAIN 


And here are the errors I am getting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\...\payroll3\payroll3B\employee.txt|1|error: expected unqualified-id before numeric constant|
C:\...\payroll3\payroll3B\employee.txt|1|error: expected `,' or `;' before numeric constant|
C:\...\payroll3\payroll3B\main.cpp|7|error: `ifstream' does not name a type|
C:\...\payroll3\payroll3B\main.cpp|9|warning: ISO C++ forbids declaration of `main' with no type|
C:\...\payroll3\payroll3B\main.cpp||In function `int main()':|
C:\...\payroll3\payroll3B\main.cpp|13|error: `cout' undeclared (first use this function)|
C:\...\payroll3\payroll3B\main.cpp|13|error: (Each undeclared identifier is reported only once for each function it appears in.)|
C:\...\payroll3\payroll3B\main.cpp|14|error: `cin' undeclared (first use this function)|
C:\...\payroll3\payroll3B\main.cpp|18|error: `fin' undeclared (first use this function)|
C:\...\payroll3\payroll3B\main.cpp|19|error: `setw' undeclared (first use this function)|
C:\...\payroll3\payroll3B\main.cpp|19|error: `setprecision' undeclared (first use this function)|
C:\...\payroll3\payroll3B\main.cpp|19|error: `endl' undeclared (first use this function)|
C:\...\payroll3\payroll3B\main.cpp|26|error: expected `)' before "employee"|
C:\...\payroll3\payroll3B\main.cpp|26|error: missing terminating " character|
||=== Build finished: 12 errors, 1 warnings ===| 


And here is what my "employee.txt" file, which is supposed to provide the Employee Number, Hours Worked and Pay Rate in order, looks like:

1
2
3
4
5
1234 38 17.50
5678 40 22.50
9098 37 15
7654 39 18
3210 40 20


What am I missing here? I was up half the night trying to figure this out and I seem to be stuck at this point. Thanks in advance, and any help would be greatly appreciated!

EDIT: Added code tags per suggestion.
Last edited on
#include <C:\...\payroll3\payroll3B\employee.txt>

That is a fantastic misunderstanding of what to do with employee.txt.

Everything in your code must be correct C++. This:

 1234 38 17.50
5678 40 22.50
9098 37 15
7654 39 18
3210 40 20

is not C++. It's some numbers. Inserting this into your code, which is what #include does, means you're inserting these numbers into the middle of your code.

You open the file with the ifstream fin object, which is fine.

This: #include <C:\...\payroll3\payroll3B\employee.txt> has no business at all being in your code.


ifstream fin.open ("employee.txt"); should be inside main function, and you've mixed up creating an object and using one that already exists. Try this instead: ifstream fin("employee.txt");


main() is not C++. int main() is.

system ( "pause" ); is expensive and dangerous. Don't use it. http://www.gidnetwork.com/b-61.html


fin.close (""employee.txt"); The close member function of an ifstream object takes no parameters. http://cplusplus.com/reference/iostream/ifstream/close/

If you want to use an ifstream, you need to #include <fstream>





Last edited on
get rid of #include <C:\...\payroll3\payroll3B\employee.txt> . #include just copy paste the content of the file so that will not work.

Next time use code tags, [code] [/code] around your code to keep the formatting intact. It makes it easier for us to read.
Thank you both very much for your input. It was very helpful, especially the links - they had great explanations about what you were saying. Now, my code looks like this:

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
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
ifstream fin("employee.txt");
int employeeid;
    float hoursworked, hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.30;
    cout<<"PLEASE ENTER THE EMPLOYEE NUMBER:";
    cin>>employeeid;
    grosspay = hoursworked * hourlyrate;
    taxamount = grosspay * TAXRATE;
    netpay = grosspay - taxamount;
while(fin>>employeeid>>hoursworked>>hourlyrate){
    cout<<"YOUR EMPLOYEE ID: "<<setw( 6 )<<setprecision( 4 )<<employeeid<<endl;
    cout<<"YOUR HOURS WORKED: "<<setw( 5 )<<setprecision( 4 )<<hoursworked<<endl;
    cout<<"YOUR HOURLY RATE: "<<setw( 6 )<<setprecision( 4 )<<hourlyrate<<endl;
    cout<<"YOUR GROSS PAY: "<<setw( 8 )<<setprecision( 4 )<<grosspay<<endl;
    cout<<"YOUR TAX AMOUNT: "<<setw( 7 )<<setprecision( 4 )<<taxamount<<endl;
    cout<<"YOUR NET PAY: "<<setw( 10 )<<setprecision( 4 )<<netpay<<endl;
    }//WHILE
    cin.get();
    return 0;
} //MAIN  


That does not have any errors in it, thanks to your suggestions. But, when I run the program, and enter one of the employee numbers (the first 4 digit number) from the .txt file, the program runs for every number in the file. Also, the numbers for Gross Pay, Tax Amount and Net Pay are in what seems to be exponential form. I am guessing that my employee.txt is causing the problem, but I am not sure. Also, should it be an ".in" file instead of ".txt"?
But, when I run the program, and enter one of the employee numbers (the first 4 digit number) from the .txt file, the program runs for every number in the file.

Why wouldn't it? Where is the code that will select the chosen employee number? Nowhere. If you want it to do something, you have to program it.

Also, should it be an ".in" file instead of ".txt"?

The name of the file is 100% meaningless to your code.
EDIT: My code now looks like the edit below. But my Gross Pay, Tax Amount and Net Pay are all showing up as 0 and the program ends after entering 1 employeeid instead of all of them. What I need to happen is it close after all 5 employeeid from employee.txt have been displayed. For instance, after you enter the first employeeid, the output is displayed, and then the program asks for the employeeid again.

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
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
int employeeid;
    float hoursworked, hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.30;
    cout<<"PLEASE ENTER THE EMPLOYEE NUMBER:";
    cin>>employeeid;
    ifstream fin("employee.txt");
    grosspay = hoursworked * hourlyrate;
    taxamount = grosspay * TAXRATE;
    netpay = grosspay - taxamount;
while(fin>>employeeid>>hoursworked>>hourlyrate){
    if( employeeid == employeeid){
    cout<<"YOUR EMPLOYEE ID: "<<setw( 6 )<<setprecision( 4 )<<employeeid<<endl;
    cout<<"YOUR HOURS WORKED: "<<setw( 5 )<<setprecision( 4 )<<hoursworked<<endl;
    cout<<"YOUR HOURLY RATE: "<<setw( 1 )<<setprecision( 6 )<<"$"<<hourlyrate<<endl;
    cout<<"YOUR GROSS PAY: "<<setw( 5 )<<setprecision( 6 )<<"$"<<grosspay<<endl;
    cout<<"YOUR TAX AMOUNT: "<<setw( 4 )<<setprecision( 6 )<<"$"<<taxamount<<endl;
    cout<<"YOUR NET PAY: "<<setw( 7 )<<setprecision( 6 )<<"$"<<netpay<<endl;
    return 0;
        }//IF
    }//WHILE
    cin.get();
    return 0;
} //MAIN 
Last edited on
if( employeeid == employeeid)

When will employeeid ever not be equal to employeeid? They're the exact same variable.

You calculate grosspay and taxamount once, at the start, before you've even read in any values. Here, look at this code:

grosspay = hoursworked * hourlyrate;
What's the value of hoursworked here? What's the value of hourlyrate?

Thank you so much, Moschops, for your continued attention. It means a lot :)

I saw what you meant, and it clicked once I thought about it. I adjusted the code to account for those rates, and now it looks like this:

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
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
int employeeid;
    float hoursworked, hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.30;
    cout<<"PLEASE ENTER THE EMPLOYEE NUMBER:";
    cin>>employeeid;
    ifstream fin("employee.txt");
while(fin>>employeeid>>hoursworked>>hourlyrate){
        if( employeeid == 0){
        cout<<"INCORRECT EMPLOYEE ID"<<employeeid<<endl;
        }
        else {
        if( employeeid == 1234){
        grosspay = hoursworked * hourlyrate;
        taxamount = grosspay * TAXRATE;
        netpay = grosspay - taxamount;
        }
        else {
        if( employeeid == 5678){
        grosspay = hoursworked * hourlyrate;
        taxamount = grosspay * TAXRATE;
        netpay = grosspay - taxamount;
        }
        else {
        if( employeeid == 9098){
        grosspay = hoursworked * hourlyrate;
        taxamount = grosspay * TAXRATE;
        netpay = grosspay - taxamount;
        }
        else {
        if( employeeid == 7654){
        grosspay = hoursworked * hourlyrate;
        taxamount = grosspay * TAXRATE;
        netpay = grosspay - taxamount;
        }
        else {
        if( employeeid == 3210){
        grosspay = hoursworked * hourlyrate;
        taxamount = grosspay * TAXRATE;
        netpay = grosspay - taxamount;
        }
        else {
        }
        }
        }
        }
        }
        }//IF
    cout<<"YOUR EMPLOYEE ID: "<<setw( 6 )<<setprecision( 4 )<<employeeid<<endl;
    cout<<"YOUR HOURS WORKED: "<<setw( 5 )<<setprecision( 4 )<<hoursworked<<endl;
    cout<<"YOUR HOURLY RATE: "<<setw( 1 )<<setprecision( 6 )<<"$"<<hourlyrate<<endl;
    cout<<"YOUR GROSS PAY: "<<setw( 5 )<<setprecision( 6 )<<"$"<<grosspay<<endl;
    cout<<"YOUR TAX AMOUNT: "<<setw( 4 )<<setprecision( 6 )<<"$"<<taxamount<<endl;
    cout<<"YOUR NET PAY: "<<setw( 7 )<<setprecision( 6 )<<"$"<<netpay<<endl;
}//WHILE
    cin.get();
    return 0;
} //MAIN 


Now my only issues are that this doesn't seem to work:

1
2
3
 if( employeeid == 0){
        cout<<"INCORRECT EMPLOYEE ID"<<employeeid<<endl;
        }


And that instead of calling for the User to enter another employeeid, it just shows the entire output of the employee.txt file. Any suggestions there?
I fixed some major styling problems. You're welcome!

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
int main() {
    int employeeid;
    float hoursworked, hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.30;
    cout<<"PLEASE ENTER THE EMPLOYEE NUMBER:";
    cin>>employeeid;
    ifstream fin("employee.txt");

    while(fin>>employeeid>>hoursworked>>hourlyrate){

        if( employeeid == 0){
            cout<<"INCORRECT EMPLOYEE ID"<<employeeid<<endl;
        }
        else if(employeeid == 1234){
            grosspay = hoursworked * hourlyrate;
            taxamount = grosspay * TAXRATE;
            netpay = grosspay - taxamount;
        }
        else if(employeeid == 5678){
            grosspay = hoursworked * hourlyrate;
            taxamount = grosspay * TAXRATE;
            netpay = grosspay - taxamount;
        }
        else if(employeeid == 9098){
            grosspay = hoursworked * hourlyrate;
            taxamount = grosspay * TAXRATE;
            netpay = grosspay - taxamount;
        }
        else if(employeeid == 7654){
            grosspay = hoursworked * hourlyrate;
            taxamount = grosspay * TAXRATE;
            netpay = grosspay - taxamount;
        }
        else if(employeeid == 3210){
            grosspay = hoursworked * hourlyrate;
            taxamount = grosspay * TAXRATE;
            netpay = grosspay - taxamount;
        }

        cout<<"YOUR EMPLOYEE ID: "  << setw(6) << setprecision(4) <<        employeeid <<endl;
        cout<<"YOUR HOURS WORKED: " << setw(5) << setprecision(4) <<        hoursworked <<endl;
        cout<<"YOUR HOURLY RATE: "  << setw(1) << setprecision(6) << "$" << hourlyrate <<endl;
        cout<<"YOUR GROSS PAY: "    << setw(5) << setprecision(6) << "$" << grosspay << endl;
        cout<<"YOUR TAX AMOUNT: "   << setw(4) << setprecision(6) << "$" << taxamount << endl;
        cout<<"YOUR NET PAY: "      << setw(7) << setprecision(6) << "$" << netpay <<endl;
    }//WHILE
    cin.get();
    return 0;
} //MAIN 
Thanks a lot! That is cool. I had to read it and compare it to mine to see what you did, and then I went ahead and ran it and it worked exactly like mine. Thanks, it looks a lot cleaner now :)
You have the user type in a number, which you store in employeeid, and two lines later you read in a number from file and store it in employeeid, overwriting the user's input which never even got used. You need to think about what you're trying to do.
I'm really sorry, Moschops, but I don't understand what you mean. I get what you are saying about the User input being stored in employeeid, but that's where I get lost. What part of the code is overwriting the User input? Is the problem with this line:

(fin>>employeeid>>hoursworked>>hourlyrate)

I also tried changing this line to get the "INCORRECT EMPLOYEE ID" to work, but it isn't working either:

1
2
3
if( employeeid != 1234, 5678, 9098, 7654, 3210){
            cout<<"INCORRECT EMPLOYEE ID"<<employeeid<<endl;
        }

Sorry to be off topic, but MORE STYLIZING!!!!!!!!!!!! :)

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
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
    int employeeid;
    float hoursworked, hourlyrate, grosspay, taxamount, netpay;
    float const TAXRATE = 0.30;
    cout<<"PLEASE ENTER THE EMPLOYEE NUMBER:";
    cin>>employeeid;
    ifstream fin("employee.txt");

    while(fin>>employeeid>>hoursworked>>hourlyrate){

        switch(employeeid) {
            case 0:
                cout<<"INCORRECT EMPLOYEE ID"<<employeeid<<endl;
                break;

            case 1234:
                grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;
                
            case 5678:
                grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;

            case 9098:
                grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;
                
            case 7654:
                grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;
                
            case 3210:
                grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;
                
        } //switch

        cout << "YOUR EMPLOYEE ID: "  << setw(6) << setprecision(4) <<        employeeid <<endl;
        cout << "YOUR HOURS WORKED: " << setw(5) << setprecision(4) <<        hoursworked <<endl;
        cout << "YOUR HOURLY RATE: "  << setw(1) << setprecision(6) << "$" << hourlyrate <<endl;
        cout << "YOUR GROSS PAY: "    << setw(5) << setprecision(6) << "$" << grosspay << endl;
        cout << "YOUR TAX AMOUNT: "   << setw(4) << setprecision(6) << "$" << taxamount << endl;
        cout << "YOUR NET PAY: "      << setw(7) << setprecision(6) << "$" << netpay <<endl;
    }//WHILE
    cin.get();
    return 0;
} //MAIN 
Last edited on
You could make this a whole lot easier on yourself by making a binary data file instead of a plaintext one. http://courses.cs.vt.edu/~cs2604/fall02/binio.html The easiest way would be to write an array of structs.
Thank you, slurpee123abc! I was actually looking at using a switch before, but we haven't gotten that far in the Course, so I wanted to stick with if/else statements. I do appreciate your input, though, as I was thinking a switch statement looks better. I will keep it in mind for when we have to use them :)

Also, with the struct array, I have to steer clear because of where we are in the Course. Plus, trying to read and understand it made my head hurt even worse :p I did bookmark the link for future use, though.
Just a suggestion, use a macro for
1
2
3
4
                grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;


coz these lines are repeated in any case. Will make your code easier to read, and shorter in length...
Excuse me? Who are you???? You're not allowed to post your problem randomly anywhere, you know that right!!! Delete your post before someone reports you, and post your problem on the forum...
Caprico, I'm not sure what you mean about the macro. I will try and look it up to figure it out.

Also, from your second reply, I am guessing that someone made a post, but then deleted it after your warning. Is that the case?
I am giving up for the night. If anyone can provide me with any more info, I would be grateful. I have to turn the entire program in tomorrow, and the outlook is not looking to good. That is not to say that I haven't received a lot of help already, of course :)
(fin>>employeeid>>hoursworked>>hourlyrate)

That line stores data from fin into the variables employeeid, hoursworked, hourlyrate. If you had anything in there before (which you did - you're using them to store the user's input), that previous data is overwritten.

employeeid != 1234, 5678, 9098, 7654, 3210
That's just a complete misunderstanding of what the , syntax dies. Don't use it. It does not mean what you think it means here.

Just a suggestion, use a macro for
1
2
3
4
 grosspay = hoursworked * hourlyrate;
                taxamount = grosspay * TAXRATE;
                netpay = grosspay - taxamount;
                break;


coz these lines are repeated in any case. Will make your code easier to read, and shorter in length...

Jesus Christ, no. No no no. Do not use a macro for repeated lines like this. We have functions now. We use functions to handle repetitive tasks like this.

Last edited on
Pages: 12