I am having trouble for this C++ Payroll

I am supposed to take advantage of the arrays by breaking the programs into separate types of units. Every unit needs to have a separate loop. I am also not supposed to use functions.

I have to read all data into arrays, compute all the overtime pays, compute all the grosspays, compute all the taxrates and the netpays, and display all of the arrays.


I typed out the code on my Dev-C++ Compiler and it is showing error. Here is the code:

#include<iostream>
#include<iomanip>
#include<fstream> //file input output system
using namespace std;
//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);

int main(){
const int MAXSIZE=100; //for maximum of 100 employees
//declaration of variables
int n;
long int id[MAXSIZE];
int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
int regularhours[MAXSIZE];
float hourlyrate[MAXSIZE], regularpay[MAXSIZE];
float overtimepay[MAXSIZE], grosspay[MAXSIZE];
float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];

//functions calls
n=readalldata(id, hoursworked, hourlyrate, MAXSIZE); //get all data
findovertimehours(hoursworked, overtimehours, n);
findovertimepay(overtimehours, hourlyrate, overtimepay, n);
findregularhours(hoursworked, regularhours, n);
findregularpay(regularhours, regularpay, grosspay, n);
findtaxrate(grosspay, taxrate, n);
findtaxamount(grosspay, taxamount, taxrate, n);
findnetpay(grosspay, netpay, taxamount, n);
printalldata(id, hoursworked, hourlyrate, overtimepay, grosspay, taxamount, netpay, n);
}//MAIN
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int counter){
} ifstream fin("payroll.dat");
counter=0;
while(fin>>setw(14)>>id[counter]>>setw(4)>>hoursworked[counter]>>setw[5]>>hourlyrate[counter])
counter++;

fin.close();
return counter;
}//READALLDATA
void findovertimehours(int hoursworked[], int overtimehours[], int n){
for(int i=0; i<n; i++)
{
if(hoursworked[i]>40)overtimehours[i]=hoursworked[i]-40;
else overtimehours[i]=0;
}//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[], float overtimepay[], int n){
for(int i=0; i<n; i++){
overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
}//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
}for(int i=0; i<n; i++){
}if(hoursworked[i]>40)regularhours[i]=40;
else regularhours[i]=hoursworked[i];
}//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[], float hourlyrate[], int n){
for(int i=0; i<n; i++){
regularpay[i]=regularhours[i]*hourlyrate[i];
}//FOR
}//FINDREGULARPAY

void findgrosspay(float regularpay[], float overtimepay[], float grosspay[], int n){
for(int i=0; i<n; i++){
}//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
for(int i=0; i<n; i++){
if(grosspay[i]>4000.00)taxrate[i]=0.40;
else if(grosspay[i]>3000.00)taxrate[i]=0.30;
else if(grosspay[i]>1000.00)taxrate[i]=0.20;
else taxrate[i]=0.10;
}//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[], float taxrate[], int n){
for(int i=0; i<n; i++){
taxamount[i]=grosspay[i]*taxrate[i];
}//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
for(int i=0; i<n; i++){
netpay[i]=grosspay[i]-taxamount[i];
}//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[], float overtimepay[], float grosspay[], float taxamount[], float netpay[], int n){

cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
<<"OVERPAY"<<"\t"<<"GROSSPAY"<<"\t"<<"TAX"<<"\t"
<<"NETPAY"<<endl;
for(int i=0; i<n; i++){

cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
<<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
<<taxamount[i]<<"\t"<<netpay[i]<<endl;
}//FOR
}//PRINTALLDATA
//end source code

Here are the error messages:

41 2 C:\Users\17345\Documents\CSCI-2021- Intro to C++ & OOP\example practice.cpp [Error] 'n' does not name a type

42 2 C:\Users\17345\Documents\CSCI-2021- Intro to C++ & OOP\example practice.cpp [Error] expected unqualified-id before 'while'

45 2 C:\Users\17345\Documents\CSCI-2021- Intro to C++ & OOP\example practice.cpp [Error] 'fin' does not name a type

46 2 C:\Users\17345\Documents\CSCI-2021- Intro to C++ & OOP\example practice.cpp [Error] expected unqualified-id before 'return'

47 1 C:\Users\17345\Documents\CSCI-2021- Intro to C++ & OOP\example practice.cpp [Error] expected declaration before '}' token

How do I fix this?
Please read https://www.cplusplus.com/articles/jEywvCM9/ and amend your post with code tags.
@soon45,
This code is very hard to read. Please see below.

1
2
3
4
5
6
7
// VERY hard to read
#include<iostream>
using namespace std;
int main(){
cout<<"Hello world!"<<endl;
return 0;
}

That is very hard to read, is it not?

Well, try this:

1
2
3
4
5
6
7
8
9
10
11
// Easier to read
#include <iostream>

using namespace std;

int main ()
{
      cout << "Hello world!" << endl;
      
      return 0;
}


Much better, no?
Now, it doesn't really matter for a short program like that, but for one that is as long as yours is, it gets very difficult to read. So, in the future, please try to use code tags like @salem c said, and use indentations like so:
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
/*------------NOT this:-------------*/
#include <iostream>
int main(){
int size = 4;
for(size_t i=0; i<size; ++i){
std::cout<<"Hello world!"<<std::endl;
}
return 0;
}

/*-------------YES this:------------*/
#include <iostream>

int main ()
{
      int size = 4;
      
      for (size_t i = 0; i < size; ++i)
      {
            // indent so it makes it easier to read
            std::cout << "Hello world!" << std::endl;
      }
      
      return 0;
}

See? The first one will compile and run, but it is oh, so hard on the eyes. The second one is much better. So, please edit your code.
Last edited on
Hello soon45,

Based on your error messages it looks like it is referring to this bit of code:
42
43
44
45
46
47
48
49
50
51
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int counter)
{
} ifstream fin("payroll.dat");
counter = 0;
while (fin >> setw(14) >> id[counter] >> setw(4) >> hoursworked[counter] >> setw[5] >> hourlyrate[counter])
counter++;

fin.close();
return counter;
}//READALLDATA 


As mentioned with proper use of blank lines it looks like:
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int counter)
{
}

ifstream fin("payroll.dat");

counter = 0;

while (fin >> setw(14) >> id[counter] >> setw(4) >> hoursworked[counter] >> setw[5] >> hourlyrate[counter])
counter++;

fin.close();

return counter;
}//READALLDATA 

Much easier to see the problem with the {}s. Also the function "findregularhours" has the same problem.

You start by saying:

I am also not supposed to use functions.


Yet the program is full of functions. Appears as you need to rethink the program or clarify what you can do.

Andy
Hello soon45,

As I read over your code I noticed the file "payroll.dat" is opened for input.

First question is how do you know it is open?

You need to provide this input file so everyone can see what is is and use the same information. If it is large 5 to 10 records will do. Just need something to work with.

If possible prefer to use "double" over "float.

When I look at: long int id I wonder if this is the correct type? I am wondering if a "std::string" would be a better choice?

Using Dev C++ I am wondering if it is set to use the C++11 standards. The 2011 standards may not be current, but it is better than what Dev starts with.

Andy
Ok, let me explain with updated information. The EMPLOYEE.txt input data file supposed to be created and store all employee name, marital Status (M or S), SSN, HW, HR data interactively when entered after run this program. I was successful compiling and running following source program. But it does not add information for more than one employee. Application closes when I hit enter after entering information for one employee.

Also, output shows calculated results for gross pay, tax, net pay. But I need to calculate overtime hours, overtime pay, and regular pay also. How to do that? Here is my update code in Dev-C++

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
89
90
91
92
93
94
95
96
#include <iomanip>
#include <iostream>
#include <fstream>      // file input output stream

using namespace std;
int main(){

    char id[100][12]; //variable for employee id

    char fname[100][14], lname[100][15], status[100][3], ssn[100][10];   //variable for first name, last name, marital status, social security number

    int hw[100],n; //variable for hourly work

    double gp[100], np[100], hr[100], oth[100], tr[100], ta[100];   //variable for gross pay, net pay, hourly rate, overtime hours, tax rate, tax amount

    int counter = 0;

    int i;

    cout << "PAYROLL INSTITUTE" << endl<<endl; //Program title

    cout<<"Enter a number:";
    cin>>n;
    cout<<"Employee ID FIRST NAME LAST NAME STAT SSN HW HR 			Press ctrl- z and Enter to end"<<endl;
    
    //cout<<"FIRST NAME LAST NAME STAT SSN HW HR OTH OTP REGP GROSS TAX NET Press ctrl- z and Enter to end"<<endl;


    while(n--)
    {
        cin>>id[counter]>>fname[counter]>>lname[counter]>>status[counter]>>ssn[counter]>>hw[counter]>>hr[counter];
        counter=counter+1;
    }

    for (i=0; i<counter; i++)  //calculating gp
    {
        gp[i] = hw[i] * hr[i];
    }
    for (i=0; i<counter; i++)   // calculating tr
    {
        if (gp[i]>500) tr[i] = .30;
        else if (gp[i]>200) tr[i] = .20;
        else tr[i] = .10;
    }
    for (i=0; i<counter; i++){      //calculating ta

    ta[i]= gp[i] * tr[i];}

    for (i=0; i<counter; i++){      //calculating np

    np[i] = gp[i] - ta[i];

    }

    cout<<setw(14)<<"Employee ID"
        <<setw(16)<<"First Name"
        <<setw(17)<<"Last Name"
        <<setw(4)<<"HW"
        <<setw(5)<<"HR"
        <<setw(6)<<"GROSS"
        <<setw(6)<<"TAX"
        <<setw(9)<<"Net Pay"<<endl<<endl;

    for (i=0; i<counter; i++){
        cout<<setw(14)<<id[i]
            <<setw(16)<<fname[i]
            <<setw(17)<<lname[i]
            <<setw(4)<<hw[i]
            <<setw(5)<<hr[i]
            <<setw(6)<<gp[i]
            <<setw(6)<<ta[i]
            <<setw(9)<<np[i]<<endl;
    }

   ofstream outdata;
   outdata.open("EMPLOYEE.txt"); // opens the file
   if( !outdata )
   { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
    for (i=0; i<counter; ++i)
    {
        outdata
        	<<setw(14)<<id[i]
        	<<setw(16)<<fname[i]
            <<setw(17)<<lname[i]
            <<setw(3)<<status[i]
            <<setw(10)<<ssn[i]
            <<setw(4)<<hw[i]
            <<setw(5)<<hr[i];

    }
    outdata.close();
    return 0;
}
Last edited on
Hello soon45,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



See about this while I look at your code.

Andy
@soon45,
Ok, first of all, your prompts are kind of lacking in detail, your first one:
"Enter a number",
should specify what exactly that number is for (number of employees).
Your second prompt-
1
2
"Employee ID FIRST NAME LAST NAME STAT SSN HW HR"
"Press ctrl- z and Enter to end"
is confusing, because it has a long string of random letters but you don't explain what they mean. Also, ctrl-z, for me at least, shuts down the running process. I am not sure if that's what you want, but that's what it does.

For the file output, the file "EMPLOYEE.txt" looks a bit wacky, the output to it needs modification.

         23543            John              DoeUNEUNEMPLOYED908-236-39908-236-39  1223.55         23543            John              DoeUNEMPLOYED908-236-39  1223.55


You should probably test your field width set functions (setw) because it looks like they're the issue. Other than those, it looks good!

Best,
max
Hello soon45,

As a start:
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
int main()
{
    constexpr unsigned int MAXSIZE{ 100 };

    char id[MAXSIZE][12]{}; //variable for employee id

    char fname[MAXSIZE][14]{}, lname[MAXSIZE][15]{}, status[MAXSIZE][3]{}, ssn[MAXSIZE][10]{}; //variable for first name, last name, marital status, social security number

    int hw[MAXSIZE]{}, n{}; // variable for hourly work.   // <--- "n" needs a better name.

    double gp[MAXSIZE]{}, np[MAXSIZE]{}, hr[MAXSIZE]{}, oth[MAXSIZE]{}, tr[MAXSIZE]{}, ta[MAXSIZE]{}; //variable for gross pay, net pay, hourly rate, overtime hours, tax rate, tax amount

    int counter = 0;

    int i;

    cout << "PAYROLL INSTITUTE\n\n"; //Program title

    cout << "Enter a number: ";
    cin >> n;


    //cout<<"FIRST NAME LAST NAME STAT SSN HW HR OTH OTP REGP GROSS TAX NET Press ctrl- z and Enter to end"<<endl;


    while (n--)
    {
        cout << "\nEmployee ID FIRST NAME LAST NAME STAT SSN HW HR Press ctrl- z and Enter to end:\n";
        cin >> id[counter] >> fname[counter] >> lname[counter] >> status[counter]
            >> ssn[counter] >> hw[counter] >> hr[counter];

        counter++;
    }

The first problem I see is with line 5. In your first code the ID was 14 numbers, here you are saying that you want 12 characters. The problem is that if you enter 12 characters there is no room for the "\0" to mark the end. Also using the "cin" can over run the size of the 2nd dimension and you will have problems later.

Back when I learned C and dealing with character arrays it was always better to make the array 1 larger than needed to leave room for the "\0" at the end.

While I am thinking about it. It is always a good idea to initialize your variables when defined.

Line 19 I choose to enter 1313 because there was no directions otherwise. Doing so would cause you to over run the 1st dimension of the 2D arrays and try to write information to something other than the array.

When you reach your for loop I would suggest entering each variable separately and use cin.get(id[counter], 12);. The problem with this is that the variable has a size of 12 and the 12 in the code means get (12 - 1) characters leaving the last element of the array for the "\0" to mark the end. This is better than filling the array or going past the end of the array.

Have a look at https://en.cppreference.com/w/cpp/io/basic_istream/get for more information.

I would concentrate on getting this much to work properly B4 you work on the rest. Until you have something to work with the rest will is pointless to work on with out good information.

So far you have mentioned that you are using "Dev C++", but not what version it is. If you can find the version number let me know.

Andy
I am using Dev C++5.11. In source code I have changed first ID to 14, also added "Enter any number" and removed "Press ctrl- z and Enter to end:" But still application closing after entering information for one employee. I have to use array for this project.

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
89
90
91
92
93
94
95
96
#include <iomanip>
#include <iostream>
#include <fstream>      // file input output stream

using namespace std;
int main(){

    char id[100][14]; //variable for employee id
    char fname[100][14], lname[100][15], status[100][3], ssn[100][10];//variable for first name, last name, marital stsus, social security, number
    int hw[100],n;//variable for hourly work
    double gp[100], np[100], hr[100], oth[100], tr[100], ta[100];//variable for gross pay, net pay, hourly rate, overtime hours, tax rate, tax amount
    int counter = 0;

    int i;

    cout << "PAYROLL INSTITUTE" << endl<<endl; //Program title

    cout<<"Enter any number:";
    cin>>n;
    cout<<"Employee ID FIRST NAME LAST NAME STAT SSN HW HR"<<endl;
    
    //cout<<"FIRST NAME LAST NAME STAT SSN HW HR OTH OTP REGP GROSS TAX NET Press ctrl- z and Enter to end"<<endl;


    while(n--)
    {
        cin>>id[counter]>>fname[counter]>>lname[counter]>>status[counter]>>ssn[counter]>>hw[counter]>>hr[counter];
        counter=counter+1;
    }

    for (i=0; i<counter; i++)  //calculating gp
    {
        gp[i] = hw[i] * hr[i];
    }
    for (i=0; i<counter; i++)   // calculating tr
    {
        if (gp[i]>500) tr[i] = .30;
        else if (gp[i]>200) tr[i] = .20;
        else tr[i] = .10;
    }
    for (i=0; i<counter; i++){      //calculating ta

    ta[i]= gp[i] * tr[i];}

    for (i=0; i<counter; i++){      //calculating np

    np[i] = gp[i] - ta[i];

    }

    cout<<setw(14)<<"Employee ID"
        <<setw(14)<<"First Name"
        <<setw(15)<<"Last Name"
        <<setw(4)<<"HW"
        <<setw(5)<<"HR"
        <<setw(6)<<"GROSS"
        <<setw(6)<<"TAX"
        <<setw(9)<<"Net Pay"<<endl;

    for (i=0; i<counter; i++){
        cout<<setw(14)<<id[i]
            <<setw(14)<<fname[i]
            <<setw(15)<<lname[i]
            <<setw(4)<<hw[i]
            <<setw(5)<<hr[i]
            <<setw(6)<<gp[i]
            <<setw(6)<<ta[i]
            <<setw(9)<<np[i]<<endl;
    }

   ofstream outdata;
   outdata.open("EMPLOYEE.txt"); // opens the file
   if( !outdata )
   { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
    for (i=0; i<counter; ++i)
    {
        outdata
        	<<setw(14)<<id[i]
        	<<setw(14)<<fname[i]
            <<setw(15)<<lname[i]
            <<setw(3)<<status[i]
            <<setw(10)<<ssn[i]
            <<setw(4)<<hw[i]
            <<setw(5)<<hr[i]<<endl;

    }
    outdata.close();
    return 0;
}



Try just pressing enter instead of ctrl-z. I think that may be your issue, because it worked fine for me. On my compiler, ctrl-z shuts down the current process, whether its debugging, compiling, or running. It may be different for yours, but try it and see.
Hello soon45,

I will get this out of the way first if you have not done this yet:

The DEV C++ that I have is version 5.11 with a build year of 2015.

To adjust the settings:
 •	Under the Tools menu choose “Compiler Options”.
 •	In the window that comes up you will see tabs for “General”, “Settings”, “Directories” and “Programs”.
 • Choose the settings tab.
 • In the next set of tabs that come up choose “Code Generation”.
 • The last line should say “Language Standard (-std).
 • On the right side of that line click on the down arrow.
 • In the list box that comes up choose “ISO C++ 11”.
 • Press “OK”.
This will let the IDE and compiler use the C++11 standards.


Line 14 should be done in the for loops not here. Unless you have a reason for this value outside the for loops, and you do not, it is better to make it a local variable in the for loop. Say that "I use it more than once" is not a good reason to make it global to the function.

For line 18 does that mean I can enter 1,000,000?

In the while loop you still have no control how many characters you can enter for any given array. You could very easily over run any of the arrays.

You still have not answered my question about the size of the ID.

Also you need to initialize your variables when they are defined. Otherwise they contain garbage that can be a problem.

This is what i have come up with 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
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
using namespace std;  // <--- Best not to use.

int main()
{
    constexpr unsigned int MAXSIZE{ 100 };

    char id[MAXSIZE][13]{"1234", "1235"}; //variable for employee id

    char fname[MAXSIZE][15]{ "Sir Charls", "Bob" }, lname[MAXSIZE][16]{ "Chaplin", "Smithe" }, status[MAXSIZE][4]{ "03", "04" }, ssn[MAXSIZE][12]{ "123456789", "123-45-6789" }; //variable for first name, last name, marital status, social security number

    int hw[MAXSIZE]{ 40, 50 }, n{ 2 }; // variable for hourly work.   // <--- "n" needs a better name.

    double grossPay[MAXSIZE]{}, NetPay[MAXSIZE]{}, hourlyRate[MAXSIZE]{10.5, 11.75}, 
        overTimeHours[MAXSIZE]{}, taxRate[MAXSIZE]{}, taxAmount[MAXSIZE]{}; //variable for gross pay, net pay, hourly rate, overtime hours, tax rate, tax amount

    int counter{2};

    //int i{};
    std::cout << std::fixed << std::showpoint << std::setprecision(2);

    cout << "PAYROLL INSTITUTE\n\n"; //Program title

    //cout << "Enter a number greater than 0 and less than " << MAXSIZE << ": ";
    //cin >> n;

    //std::cin.ignore();

    //cout<<"FIRST NAME LAST NAME STAT SSN HW HR OTH OTP REGP GROSS TAX NET Press ctrl- z and Enter to end"<<endl;


    //while (n--)
    //{
    //    cout << "\nctrl- z and Enter to end:\n";

    //    cout << "\nEmployee ID: ";
    //    cin.get(id[counter], 12);
    //    std::cin.ignore();

    //    cout << "\nEmployee First Name: ";
    //    std::cin.get(fname[counter], 15);
    //    std::cin.ignore();

    //    cout << "\nEmployee Last Name: ";
    //    std::cin.get(lname[counter], 16);
    //    std::cin.ignore();

    //    cout << "\nEmployee Status: ";
    //    std::cin.get(status[counter], 4);
    //    std::cin.ignore();

    //    cout << "\nEmployee SSN (123456789 or 123-45-6789): ";
    //    std::cin.get(ssn[counter], 12);
    //    std::cin.ignore();

    //    cout << "\nHours Worked: ";
    //    std::cin >> hw[counter];
    //    std::cin.ignore();

    //    cout << "\nEmployee Hourly Rate: ";
    //    std::cin >> hourlyRate[counter];
    //    std::cin.ignore();

    //    counter++;
    //} 

After I got the while loop working I put the comments on it and changes the initialization of the variables. This is a little trick so that you do not have to enter everything every time the program runs. This allows you to focus on the other parts of the program to get them working.

When I first let the program continue I noticed that the "cout" statement for the headings does not work well. Actually it is kind of backwards.

With "setw" the default setting is "std::right" unless changed. Your headings work better if you set "std::left". Then in the for loop you need to change between left and right.

What I ended up with looks like this.

PAYROLL INSTITUTE

Employee ID   First Name      Last Name         HW    HR    GROSS   TAX     Net Pay

1234          Sir Charles     Chaplin           40  10.50   420.00  84.00   336.00
1235          Bob             Smithe            50  11.75   587.50 176.25   411.25



And for the output file I did this:

1234,Sir Charles,Chaplin,03,123456789,40 10.5
1235,Bob,Smithe,04,123-45-6789,50 11.75


Using the commas in the file will make it easier to read when the time comes.

Sometimes for the headings I find it easier to create 1 long string and separate the headings with spaces. then in the for loop I use the "setw"s to give the proper format. Either way can work.

I added line 19 to format the floating point numbers to line up correctly.

I have yet to look into all the calculations, but they appear to give a usable output.

Andy
Thanks Andy! I just reset my Dev-C++ compiler as you have suggested.
Then I have tried to run your program to test, but the application came without giving any option to enter data, and closes when hit enter. I made bottom curly brace active removing "/" and added #include <iomanip>
#include <iostream>
#include <fstream> on top of the source code before compile and run.
Then I ran removing all comments, the application takes input, but no output created. What I am missing? Please let me know.

Last edited on
If you ran his program, you should note that it isn't finished-it doesn't have any outputs.
Hello soon45,

agent max is correct I only showed the input part and what you could do.

I would scrap you while loop as it is not safe and could be a potential problem.

The code I have in comments is better and less likely to to overflow any of the arrays unless yo get a name that is larger than what you have room.

I did catch 1 change that needs to be made. When "id" is defined the second dimension needs to be 13 if you want 12 characters for the ID or 15 if you want 14 characters for the ID. Also line 36 the number needs to match the number used to define the array.

I ran the code in my "Dev C++", same version as you have, and it ran with no problems.

Putting a closing } to end "main" would allow the code to run, but you would not see anything from it except the prompts and what you input.

Andy
Topic archived. No new replies allowed.