# Thread: Newbie - i need a total of list of numbers

1. ## Newbie - i need a total of list of numbers (I posted code)

I am working on a project that calculates gross pay and net pay from an imported file that contains 4 employees and their rate per hour and their number of hours worked. I can import the file and do the calculations perfectly. The part I can't do.

I am supposed to sum up all of the employees gross pay and net pay to get the total for the company. So the file you are looking at looks something like this for example:
employee gross pay
1 50
2 100
3 75

I need to sum the 50+100+75 to get a total but my program needs to sum a thousand numbers if need be. My problem is that I am just using a standard; totgross=totgross +totgross but that is just adding 75 and 75.

2. It would help if you posted the code that you've written. That will give us an idea of what you've already tried.

3. How are you storing the gross and net pay? Are you saving them in a container of some sort (array/vector etc...) or are you simply trying to keep running totals as you read in your employee data. Show some code!

4. yeah, that's on a different computer. can't post it right now.

I can tell you this....I import a file that has the employee information and do calculations to it to get gross, net and all taxes. that spits out everything perfectly. after the calculations i run a PrintSummary program that messes up.

if this is still no help i'll post my code later.

5. trying to keep running.

6. don't worry m8 , i can;t tell you , 50 + 100 + 75 = 225 , problem solved :P

but seriuos , if you do totgros = totgros + totgros and and the awnser is 150 it will be the same as 75 + 75 . what i wouyld suggest ( i am still a noob too but ) just make 3 variables so

Code:
```int a ;
int b ;
int c ;

totgros = a + b + c```
or if it already holds a value ( for example c = 75 ) just let it go like :

Code:
`totgros = a + b`
as simple as it is . if this didn;t solve the problem i must have understood your question wrong , it would help if you post the code you have so far so we Xactly see what you do mean.

7. hmm don't think you ment that after all .

medic ! I NEED CODE ! CODE !

8. Something along the lines of an integer storing how many employees, a loop, and an array?
Code:
```const int employees = 10; //ten employees
int array[10] //this stores salary of each employee, simply read in the values
int total;
for( int I = 0; I < employees; I++ )
{
Total += array[I];
}```
Um...?
- SirCrono6

9. ## here is my code.

at the bottom where i keep messing up i had to insert the "//" b/c i could not get past them.

Code:
```#include <iostream>
#include <iomanip>
using namespace std;
#include <fstream>

void ProcessPayroll(float&, float&, float&, float&, float&, ifstream&, ofstream&);
int ProcessEmployee(int&, float&, int&, ifstream&);
void PrintSummary(float, float, float, float, float, ofstream&);
int main()
{
ifstream infile;
infile.open ("lab1.txt");
if(infile.fail())
{
cout << "failing to open input file\n";
getchar();
exit(1);
}

ofstream outfile;
outfile.open ("out1.txt");
if(outfile.fail())
{
cout << "failing to open output file\n";
getchar();
exit(1);
}
float totgross, totss, totfed, totstate, totnet;
ProcessPayroll(totgross, totss, totfed, totstate, totnet, infile, outfile);
getchar();
return 0;
}

int ProcessEmployee(int& EmpNum, float& Rate, int& Hours, ifstream& input)
{
if(input >> EmpNum >> Rate >> Hours)
return 1;
else
return 0;
}

void ProcessPayroll(float& totgross, float& totss, float& totfed, float& totstate, float& totnet, ifstream& input, ofstream& outfile)
{
float Rate;
int EmpNum, Hours;
char flag = ' ';

cout << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  "
<< "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";
outfile << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  "
<< "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";

while(ProcessEmployee(EmpNum, Rate, Hours, input))
{
if(Hours > 40)
{
totgross=((Rate * 1.5)*(Hours - 40)) + (Rate * 40);
flag = '\$';
}
else if(Hours > 35)
{
totgross=Rate * Hours;
flag = ' ';
}
else if(Hours <= 35)
{
totgross=(Rate + .15) * Hours;
flag = '*';
}

totss = totgross*.07;
totfed = totgross*.16;
totstate = totgross*.0542;
totnet = totgross - (totss + totfed + totstate);

cout << setw(8) << EmpNum << "  "
<< setw(5) << Hours  << flag << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";

outfile << setw(8) << EmpNum << "  "
<< setw(5) << Hours  << flag << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
}
PrintSummary(totgross, totss, totfed, totstate, totnet, outfile);

return;
}

void PrintSummary(float totgross, float totss, float totfed, float totstate, float totnet, ofstream& outfile)
{
//float totgross, totss, totfed, totstate, totnet;

cout << "\n\n\nSummary - Totals For All Employees" << "\n\n  "
<< "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   "
<< "State Tax" << "   " << "Soc Security\n";

outfile << "\n\n\nSummary - Totals For All Employees" << "\n\n  "
<< "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   "
<< "State Tax" << "   " << "Soc Security\n";

//while(ProcessPayroll(totgross, totss, totfed, totstate, totnet, input, outfile))
{
totgross=totgross+totgross;
totnet=totnet+totnet;
totfed=totfed+totfed;
totstate=totstate+totstate;
totss=totss+totss;

cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";

outfile << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";

}
outfile.close();
return;
}```

10. Code:
```#include <iostream>
#include <iomanip>
#include <fstream>  // Moved this from two lines below
using namespace std;

void ProcessPayroll(float&, float&, float&, float&, float&, ifstream&, ofstream&);
int ProcessEmployee(int&, float&, int&, ifstream&);
void PrintSummary(float, float, float, float, float, ofstream&);
int main()
{
ifstream infile;
infile.open ("lab1.txt");
if(infile.fail())
{
cout << "failing to open input file\n";
getchar();
exit(1);
}

ofstream outfile;
outfile.open ("out1.txt");
if(outfile.fail())
{
cout << "failing to open output file\n";
getchar();
exit(1);
}
float totgross, totss, totfed, totstate, totnet;
// Added line below, should initialize values before you use them
totgross = totss = totfed = totstate = totnet = 0.0f;
ProcessPayroll(totgross, totss, totfed, totstate, totnet, infile, outfile);
getchar();
return 0;
}

int ProcessEmployee(int& EmpNum, float& Rate, int& Hours, ifstream& input)
{
if(input >> EmpNum >> Rate >> Hours)
return 1;
else
return 0;
}

void ProcessPayroll(float& totgross, float& totss, float& totfed, float& totstate,
float& totnet, ifstream& input, ofstream& outfile)
{
float Rate;
int EmpNum, Hours;
char flag = ' ';
float gross, net, ss, state, fed;

cout << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  "
<< "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";
outfile << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  "
<< "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";

while(ProcessEmployee(EmpNum, Rate, Hours, input))
{
if(Hours > 40)
{
gross = ((Rate * 1.5)*(Hours - 40)) + (Rate * 40);
flag = '\$';
}
else if(Hours > 35)
{
gross = Rate * Hours;
flag = ' ';
}
else if(Hours <= 35)
{
gross = (Rate + .15) * Hours;
flag = '*';
}

ss = gross * 0.07;
fed = gross * 0.16;
state = gross * 0.0542;
net = gross - state - fed - state;

totss += ss;
totfed += fed;
totstate += state;
totnet += net;
totgross += gross;

cout << setw(8) << EmpNum << "  "
<< setw(5) << Hours  << flag << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << gross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << net << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << fed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << state << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << ss << "\n";

outfile << setw(8) << EmpNum << "  "
<< setw(5) << Hours  << flag << " "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << gross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << net << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << fed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << state << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << ss << "\n";
}

PrintSummary(totgross, totss, totfed, totstate, totnet, outfile);

return;
}

void PrintSummary(float totgross, float totss, float totfed, float totstate, float totnet,
ofstream& outfile)
{
//float totgross, totss, totfed, totstate, totnet;

cout << "\n\n\nSummary - Totals For All Employees" << "\n\n  "
<< "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   "
<< "State Tax" << "   " << "Soc Security\n";

outfile << "\n\n\nSummary - Totals For All Employees" << "\n\n  "
<< "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   "
<< "State Tax" << "   " << "Soc Security\n";

// Get rid of all this below in blue
//while(ProcessPayroll(totgross, totss, totfed, totstate, totnet, input, outfile))
{
totgross=totgross+totgross;
totnet=totnet+totnet;
totfed=totfed+totfed;
totstate=totstate+totstate;
totss=totss+totss;

cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";

outfile << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
<< setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
<< setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";

// Get rid of the following in blue
}

outfile.close();
return;
}```
Note, your main function has no need to keep the totfed, totss, totstate, totgross, and totnet variables local to it. main does not use them anywhere so they should be moved into the ProcessPayroll function. This means that the function prototype should be changed to only have the infile and outfile file stream objects.