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:
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++;
@soon45,
This code is very hard to read. Please see below.
1 2 3 4 5 6 7
// VERY hard to read
#include<iostream>
usingnamespace 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>
usingnamespace 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:
/*------------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.
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: longint 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.
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++
#include <iomanip>
#include <iostream>
#include <fstream> // file input output stream
usingnamespace 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;
elseif (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;
}
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.
@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!
int main()
{
constexprunsignedint 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.
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.
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.
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.
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.
usingnamespace std; // <--- Best not to use.
int main()
{
constexprunsignedint 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
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.
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.
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.